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 10 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:

01bans = {}
02 
03game.Players.LocalPlayer.Chatted:connect(function(ban)
04if ban:lower():sub(1,3) == "/b " then
05plr = ban.sub(ban,4)
06player = game.Players:FindFirstChild(plr)
07if player ~= nil then
08for i = 1, #bans do
09if plr ~= bans[i] then
10table.insert(bans,plr)
11plr:remove()
12end
13end
14end
15end
View all 24 lines...

2 answers

Log in to vote
4
Answered by 10 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

01bans = {}
02 
03game.Players.LocalPlayer.Chatted:connect(function(ban)
04    if ban:lower():sub(1,3) == "/b " then
05        plr = ban:sub(4)
06        player = game.Players:FindFirstChild(plr)
07        if player ~= nil then
08            player:Kick()
09            local bannedAlready = false
10            for i = 1, #bans do
11                if plr == bans[i] then
12                    bannedAlready = true
13                    break
14                end
15            end
View all 29 lines...

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 — 10y
0
Thank you, @gskw, for the tip!! NoahWillCode 370 — 10y
Ad
Log in to vote
2
Answered by 10 years ago

Just a few problems in your code. I have outlined them in the code below.

01bans = {}
02 
03game.Players.LocalPlayer.Chatted:connect(function(ban)
04    if ban:lower():sub(1,3) == "/b " then
05        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())
06        player = game.Players:FindFirstChild(plr)
07        if player ~= nil then
08            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.
09            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.
10        end
11    end
12end)
13 
14game.Players.PlayerAdded:connect(function(plr)
15    for i,v in pairs(bans) do --Changed from numeric for loop to generic for loop.
16        if v == plr.Name then --If the value in the bans table is equal to the player's name then...
17            plr:Kick() --The kick method properly disconnects the player from the server.
18        end
19    end
20end)
0
Also, didn't notice Code's post whilst I was typing my answer, apologies. Spongocardo 1991 — 10y
0
It's ok. Both answers were just as good :) General_Scripter 425 — 10y
0
You're welcome. :) Spongocardo 1991 — 10y
0
Good job, Spongo! But just a quick tip - numeric for loops are faster than generic for loops. NoahWillCode 370 — 10y
0
@Code Alright, thanks for the tip. Spongocardo 1991 — 10y

Answer this question