I wanted to make an in game ban command/script. This is what I have but it doesn't work and nothing comes out in the output for me to read:
bans = {} game.Players.LocalPlayer.Chatted:connect(function(ban) if ban:lower():sub(1,3) == "/b " then plr = ban.sub(ban,4) player = game.Players:FindFirstChild(plr) if player ~= nil then for i = 1, #bans do if plr ~= bans[i] then table.insert(bans,plr) plr:remove() end end end end end) game.Players.PlayerAdded:connect(function(plr) for i = 1, #bans do if plr.Name == bans[i] then plr:remove() end end end)
string
. You needed to remove the Player
. You were using variable plr
not player
. I fixed that for you.remove()
line was inside of the if plr ~= bans[i] then
block, which was inside your for loop
. You would have noticed this if you had formatted your code.plr string
everytime there was already a string in the bans
table that WASN'T equal to plr
. I'm assuming this was just a small mistake, but your script wouldn't have worked. Now, I'm just checking to see if the player is already banned. At the end of the loop, if they are not banned yet, I am banning them by putting their name into the array.bans = {} game.Players.LocalPlayer.Chatted:connect(function(ban) if ban:lower():sub(1,3) == "/b " then plr = ban:sub(4) player = game.Players:FindFirstChild(plr) if player ~= nil then player:Kick() local bannedAlready = false for i = 1, #bans do if plr == bans[i] then bannedAlready = true break end end if not bannedAlready then table.insert(bans,plr) end end end end) game.Players.PlayerAdded:connect(function(plr) for i = 1, bans do if plr.Name == bans[i] then plr:Kick() end end end)
FORMAT YOUR CODE!!!
EDITS I edited my code to improve it a little bit.
Just a few problems in your code. I have outlined them in the code below.
bans = {} game.Players.LocalPlayer.Chatted:connect(function(ban) if ban:lower():sub(1,3) == "/b " then plr = ban:sub(4) --You tried to use string.sub on the ban variable, but forgot to put in string. I made it simpler by using the method above (:sub()) player = game.Players:FindFirstChild(plr) if player ~= nil then table.insert(bans,plr.Name) --You don't need to check if the player is not in the bans table, they wouldn't be able to get in the game otherwise. Also, just add in the player's name and not the actual player. player:Kick() --The kick method properly disconnects the player from the server. Also, you were trying to remove the player's name string, not the player itself. end end end) game.Players.PlayerAdded:connect(function(plr) for i,v in pairs(bans) do --Changed from numeric for loop to generic for loop. if v == plr.Name then --If the value in the bans table is equal to the player's name then... plr:Kick() --The kick method properly disconnects the player from the server. end end end)