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

Why is my script not working?

Asked by 9 years ago

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)

2 answers

Log in to vote
4
Answered by 9 years ago

Four Things

  1. Your code was not formatted. Format. Your. Code. It's proper practice and makes spotting errors easier.
  2. You were removing a string. You needed to remove the Player. You were using variable plr not player. I fixed that for you.
  3. Your 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.
  4. You were adding the 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.

Here is the working script

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.

1
I suggest you to use the Markdown ordered list for formatting the answer as the site supports that. gskw 1046 — 9y
0
Thank you, @gskw, for the tip!! NoahWillCode 370 — 9y
Ad
Log in to vote
2
Answered by 9 years ago

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)
0
Also, didn't notice Code's post whilst I was typing my answer, apologies. Spongocardo 1991 — 9y
0
It's ok. Both answers were just as good :) General_Scripter 425 — 9y
0
You're welcome. :) Spongocardo 1991 — 9y
0
Good job, Spongo! But just a quick tip - numeric for loops are faster than generic for loops. NoahWillCode 370 — 9y
0
@Code Alright, thanks for the tip. Spongocardo 1991 — 9y

Answer this question