Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my script not working?

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

✓ Accepted answer

#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.

2 comments

1

I suggest you to use the Markdown ordered list for formatting the answer as the site supports that.

0

Thank you, @gskw, for the tip!!

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)

5 comments

0

Also, didn't notice Code's post whilst I was typing my answer, apologies.

0

It's ok. Both answers were just as good :)

0

You're welcome. :)

0

Good job, Spongo! But just a quick tip - numeric for loops are faster than generic for loops.

0

@Code Alright, thanks for the tip.

This discussion is preserved as it was. New answers, comments, and votes are closed.