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)
1 comment
Ok, this doesn't answer your question, but in your script on line 40, you use :remove() to kick the player. Just use :Kick() instead. :Kick() even offers the parameter for you to put in a kick message like :Kick("User is banned")