Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my ban/kick script not working (table insert error) ??!?!

Asked by 6 years ago

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?!!

1
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") Troidit 253 — 6y

2 answers

Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
6 years ago
Edited 6 years ago

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)
0
You accepted an answer that does not work? Please learn to use SH lukeb50 631 — 6y
0
how does my answer not work? R_alatch 394 — 6y
Ad
Log in to vote
2
Answered by
lukeb50 631 Moderation Voter
6 years ago

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
0
i dont know if youre mad your answer didnt get accepted or what but i literally tested my script and it works fine. for you to downvote me is ridiculous. R_alatch 394 — 6y

Answer this question