Why is my script not being able to kick a user? - lua

This script is designed so that when you say "!kick [user]" it kicks that user from the game. But, when I test it out, nothing happens with no errors in the output box. Whats going on?
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(player,message)
if player.Name == "playername" then
local words = string.split(message," ")
if string.lower(words[1]) == "!kick" then
words[2]:Kick("You have been kicked.")
end
end
end)
end)
Thanks

The reason nothing is happening right now is because your very first check is failing. You have the parameters for the Player.Chatted connection backwards. The message comes first and the recipient comes second. So what you've named player is actually the message and if player.Name == "playername" then is likely failing because strings don't have a Name property and player.Name is nil.
After that, the next issue you will run into is that after you've split the command into two parts, the second half of the command is still a string, and not a Player. So you need to find a Player with a matching name as the input string.
Try something like this:
local Players = game.Players
local function findPlayerByName(name)
-- escape if nothing is provided
if name == nil then
return nil
end
-- check if any players match the name provided
local allPlayers = Players:GetPlayers()
for _, player in ipairs(allPlayers) do
if player.Name == name or string.lower(player.Name) == name then
return player
end
end
-- couldn't find any players that matched
return nil
end
-- define all the players that can use commands
local admins = {
["playername"] = true,
}
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message, recipient)
if admins[player.Name] then
local words = string.split(message, " ")
local cmd = string.lower(words[1])
if cmd == "!kick" then
local playerName = words[2]
local targetPlayer = findPlayerByName(playerName)
if targetPlayer then
targetPlayer:Kick("You have been kicked.")
end
end
end
end)
end)

Related

“Value is not a valid member of model” error, when it 100% is

So, I’m trying to make it so when a player claims a tycoon, the number of the tycoon is saved in an intvalue that gets parented to their character. There is a button that should only function if the player owns the tycoon it is in. The value is name “Tycoon”. When I try to do
local click = workspace.Button1.ClickDetector
click.MouseClick:Connect(function(player)
if player.Character.Tycoon.Value == 1 then
…
end
end)
the game throws me an error:
“Tycoon is not a valid member of Model “Workspace.PlatinumAdventurer(my user)””
When I run the project, I can see that the intvalue is 100% in the character.
I have tried using :waitforchild, but it doesn’t work. I’ve also tried to, instead of doing player.Character, doing
local playerName = player.Name
…
if workspace.playerName.Tycoon.Value…
Any help would be appreciated, thank you?
It might have to do with the model structure.
You can try checking if player has a character first, then find the IntValueTycoon parented to the character. Once the IntValue is found, it should retrieve it's actual value and perform action if tycoonValue == 1
local click = workspace.Button1.ClickDetector
click.MouseClick:Connect(function(player)
local character = player.Character
if character and character:FindFirstChild("Tycoon") then
local tycoonValue = character.Tycoon.Value
if tycoonValue == 1 then
...
end
end
end)

The door data is not saved in Roblox

I wrote a door save system.
That is, if the user previously bought them, then when re-entering the game, they must be open.
My code works, but the door doesn't save at all.
-- DoorsDataStore
-- Save Stats Doors
local opend = false
local datastorage = game:GetService("DataStoreService")
local isitopen_1 = datastorage:GetDataStore("Door")
game.Players.PlayerAdded:Connect(function(player)
local boolValueDoors = Instance.new("Folder")
boolValueDoors.Name = "BoolValueDoors"
boolValueDoors.Parent = player
local door_1 = Instance.new("BoolValue")
door_1.Parent = boolValueDoors
door_1.Name = "BoolValueDoor_1"
door_1.Value = isitopen_1:GetAsync(player.UserId)
print("True or False")
print(player.BoolValueDoor_1.Value)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, erromsg = pcall(function()
isitopen_1:SetAsync(player.UserId, player.BoolValueDoor_1.Value)
end)
if erromsg then
warn("Error")
end
end)
-- TouchDoor
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if (humanoid ~= nil) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.leaderstats.Coins.Value >= script.Parent.Price.Value then
player.leaderstats.Coins.Value -= script.Parent.Price.Value
player.leaderstats.Level.Value += 1
script.Parent:Destroy()
player.BoolValueDoors.BoolValueDoor_1.Value = true
print("Save Door")
end
end
end)
I tried writing this code in different ways, in different versions, tried through validation. My code still doesn't do what I want.
There are multiple possible problems I see:
1.
SetAsync takes a string as the first argument, you are giving it a number value. To fix this use tostring(player.UserId)
2.
When the player first joins, the value will be set to nil, because there is no data in the datastore under the UserId, and nil is not a boolean.
I’m not sure if these are actual issues and I currently cannot check if these are real problems because I’m not on my computer but they may be something you want to change.
Also it would be nice to know if you encountered any errors when executing the code.
You should also make trigger a remote event that opens the door on the clientside in the PlayerAdded function if the value is true.

Roblox In-Game Ban System

I am trying to make an in-game system for banning players. I have a button that fires a remote event with a player's name and a message for why they were banned. But every time I hit the button, I get this error :
ServerScriptService.Event_Handler:21: attempt to call a nil value
I have no idea why this is not working can someone help me understand what's going wrong?
EVENT_HANDLER
local dss = game:GetService("DataStoreService")
local bands = dss:GetDataStore("banDataStore")
BanPlayer.OnServerEvent:Connect(function(player, playertoban, reason)
local pui = player.UserId
local success, errormessage = pcall(function()
bands:SetAsync("Banned-", pui, true)
end)
if success then
print("Player Successfuly Banned")
end
game.Players:FindFirstChild(playertoban):Kick(reason)
end)
Since you are searching for players by name, it's possible that you could be spelling the name wrong. In that case, game.Players:FindFirstChild() will return nil. You can sanitize this call by making sure that the player exists before calling Kick()
Also, as a side note, it looks like you are banning the player that calls the BanPlayer RemoteEvent, not the one whose name is stored in playertoban.
local dss = game:GetService("DataStoreService")
local bands = dss:GetDataStore("banDataStore")
BanPlayer.OnServerEvent:Connect(function(player, playertoban, reason)
-- check that playertoban is a real player's name
local bannedPlayer = game.Players:FindFirstChild(playertoban)
if not bannedPlayer then
warn("Could not find a player named " .. playertoban)
return
end
-- record their user-id so we can ban them when they rejoin
local pui = bannedPlayer.UserId
local success, errormessage = pcall(function()
bands:SetAsync("Banned-", pui, true)
end)
if success then
print(playertoban .. " Successfully Banned")
else
warn(string.format("Failed to ban %s permanently with error : %s", playertoban, errormessage))
end
-- remove them from the game
bannedPlayer:Kick(reason)
end)

How to fix an end expected error near eof

I made a cash for kill script in Roblox, but wanted to go further than that and implement a script where when a player has a gamepass, then that player would recieve more cash than another normal player would.
This is for a battle royale styled game in Roblox, and when I playtested it, there were no errors, but the script didn't work.
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
local increment = 50
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player,7382818)then
increment = increment + 50
end
player:WaitForChild("Humanoid").Died:connect(function()
local tag = player.Humanoid:FindFirstChild("creator")
if tag ~= nil then
local killer = tag.Value
if killer ~= nil then
-- Find the killer's leaderstats folder
local killerStats = killer:FindFirstChild("leaderstats")
if killerStats ~= nil then
-- Find the killer's Cash IntValue
local killerCash = killerStats:FindFirstChild("Cash")
-- Increase cash as before
killerCash.Value = killerCash.Value + increment
end
end
end
end)
I expected a VIP player, who has the gamepass, to receive more cash, but when I tested it, no player received any cash for killing another player at all.
How to fix an end expected error near eof
If the Lua interpreter complains about a missing end you`re missing it somewhere.
Read through your code and make sure everything that is supposed to be closed with an end has one. Read through the Lua Reference Manual to find out which keywords need an end.
In your code it's if statements and function definitions. Checking each pair from inside out you'll end up one end) short to close this game.Players.PlayerAdded:connect(function(player) as mentioned in the comments.

ROBLOX Kill command to kill specified player

I want to make a command that would kill a player you specify..
Let's say I type: kill/Paul. Now I want to kill the player with the name Paul.
This is my command Script:
local player = ...
game.Players.PlayerAdded:connect(function(player) --this gets the player that connected
player.Chatted:connect(function(message) --this function executes when the player type into chat
--commands are here
if player.Name == "TominoCZ" or player.Name == "nathancain" or player.Name == "block100000" then
if message == "kill/me" then
player.Character.Head:remove()
end
if message == "ff/me" then
if player.Character:findFirstChild("ForceField") then
player.Character.ForceField:Destroy()
end
Instance.new("ForceField").Parent = player.Character
end
if message == "unff/me" then
if player.Character:findFirstChild("ForceField") then
player.Character.ForceField:Destroy()
end
end
end
end)
end)
Now you can see that I already have a command that will kill the play that executed it.
But how can I kill a different player by specifying the player's name after the "kill/"?
This command script might look too long or not too pro, but atleast I know and understand what it does.
So any ideas?
You can do something like this:
local player = ...
game.Players.PlayerAdded:connect(function(player) --this gets the player that connected
player.Chatted:connect(function(message) --this function executes when the player type into chat
--commands are here
if string.sub(message,1,string.len("kill/"))=="kill/" then --check the message if it starts with command "kill/"
if string.sub(message,string.len("kill/"))==player.Name then --kill the player with the name
player.Character.Head:remove()
end
end)
end)
I don't know Lua so there might be syntax errors, but the overall idea is that you use string.sub method to divide your message into 2 parts: the command part and the info part. If the command part equals to "kill/", then find the player with the name specified in the info part, and kill him! (Or behead him... I don't play ROBLOX :D)

Resources