If i say ban/Lolamtic it says error in line 34
admins = {"Lolamtic"} banned = {} function isPlayerAdmin(name) for i,v in pairs(admins) do if name == v then return true end end end function playerBanned(name) end function chatted(msg,rec) if string.sub(msg,1,5) == "kill/" then local player = game.Players:FindFirstChild(string.sub(msg,6)) if player then if player.Character then player.Character.Humanoid.Health = 0 end end elseif string.sub(msg,1,5) == "kick/" then local player = game.Players:FindFirstChild(string.sub(msg,6)) if player then player:Kick() end elseif string.sub(msg,1,4) == "ban/" then local player = game.Players:FindFirstChild(string.sub(msg,6)) if player then player:remove() end table.insert(banned,player.Name) end end function playerAdded(newplayer) if playerBanned(newplayer.Name) then newplayer:remove() elseif isPlayerAdmin(newplayer.Name) then newplayer.Chatted:connect(chatted) else print("player is not admin!") end end game.Players.PlayerAdded:connect(playerAdded)
Can someone fix this and say why it is not working?!!
Problem 1
The problem is fairly simple, you're trying to insert a nil value into the banned table. Whenever you kick someone from a game, they are essentially not there anymore, or nil. Then, when you attempt to add them to the table since you're kicking them before this is done, it will error saying player is nil. To fix this, put "table.insert(banned, player.Name)" before you kick the player.
Problem 2
Another problem is where you say "string.sub(msg, 5)", this moves up to the part in the string where you say "/" because it's the 5th character in the message that is sent. You want to make this 6 because that will read what is said after the slash. Also, use 5 for ban/ since it's 4 characters rather than the 5 used for kill/ and kick/.
Problem 3
There's 2 layers of wrong with when you use remove(). Remove() is deprecated meaning Destroy() is what you should be using instead. Also, while Destroy() will get rid of the player, it's better to use Kick() because it has a parameter that will allow you to display a message in the red box that shows when you're kicked.
Wiki pages: table.insert, string.sub, Kick()
Code applying all these changes:
admins = {"Lolamtic"} banned = {} function isPlayerAdmin(name) for _, playername in pairs(admins) do if name == playername then return true end end end function playerBanned(name) end function chatted(msg) if string.sub(msg, 1, 5) == "kill/" then local player = game:GetService("Players"):FindFirstChild(string.sub(msg, 6)) if player then if player.Character then player.Character:FindFirstChild("Humanoid").Health = 0 end end elseif string.sub(msg, 1, 5) == "kick/" then local player = game:GetService("Players"):FindFirstChild(string.sub(msg, 6)) if player then player:Kick("You have been kicked!") end elseif string.sub(msg, 1, 4) == "ban/" then local player = game:GetService("Players"):FindFirstChild(string.sub(msg, 5)) if player then table.insert(banned, player.Name) player:Kick("You have been banned!") end end end function playerAdded(newplayer) if playerBanned(newplayer.Name) then newplayer:Kick("You're banned from this server!") elseif isPlayerAdmin(newplayer.Name) then newplayer.Chatted:Connect(chatted) end end game:GetService("Players").PlayerAdded:Connect(playerAdded)
Little things will get you when you copy and paste code, watch out for them!
Also, telling us the whole error helps a lot.
The problem is actually several lines up from line 34, on line 30.
The reason this line errors and not the other times you find a player is because "Kick/" and "Kill/" are both 5 characters long, but "Ban/" is only 4 characters long, so the script removes the first letter of their name then looks for them, but can't find them. And here is where you made your second error. Your check to see if the player exists covers the :remove()
(This is VERY bad practice, more on that below), but not the table.insert()
, so it tries to add them to the table but "player" is nil. Even if you enter a valid name, You remove the player instance before you add them to the table, so that would error as well.
As for :remove()
, its bad in 2 ways. If you want to remove things, you should always use :Destroy()
. If you are removing a player, it is better to use player:Kick(message)
as this kicks them and you can send them a message, such as "You have been banned".
Code that will work:
elseif string.sub(msg,1,5) == "kick/" then local player = game.Players:FindFirstChild(string.sub(msg,5)) if player then table.insert(banned,player.Name) player:Kick() end end