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

Player not being kicked from the game even though there banned?

So I made a ban script and I put my own name in the table and my alt, but when I joined, I wasn't banned. Can someone help?

local banned = {"CodeWon","Code1Won"} --List of players that are banned

game.Players.PlayerAdded:Connect(function(player)
	if player.Name == banned then --Checking if banned
		player:Kick("Banned from the Game") --Kick if banned
		print(player.Name.." is banned")
	else
		warn("Error")
		print("Player isn't banned")
	end
end)

1 comment

0

i suck at tables but i think you need to do something like table.find or something like that

4 answers

i think it goes like this

local banned = {"CodeWon","Code1Won"} --List of players that are banned

game.Players.PlayerAdded:Connect(function(player)
    if player.Name == table.find(banned,player.Name) then --Checking if banned
        player:Kick("Banned from the Game") --Kick if banned
        print(player.Name.." is banned")
    else
        warn("Error")
        print("Player isn't banned")
    end
end)

Line number four might be the issue. If you want to see whether a value is in a table you use table.find. If instead you are using a dictionary, you use dictionary[key].

If you don't know the difference then go here.

You made no actual attempt to check for the value inside of the table.

Essentially, you are comparing a string to a table by doing player.Name == banned. You are not comparing any values of the table to the name of the player. Since the table you have is an array, these values have numeric indices (1 and 2, respectively, since Lua is insane).

This also means you can use a numeric for loop using the length operator, #:

for i = 1, #banned do
end

And because you can use a numeric for loop, you can refer to these values using numeric indices, such as banned[1], which would refer to CodeWon.

For searching through a table, I would not use table.find(). I understand Roblox makes things easier for their developers, but the more you grow accustomed to using for loops, the better you will get at using them for iterating, especially for times when you actually need to iterate. Plus, table.find() literally uses a for loop anyway. I don't see a valid point in using this function unless you want to stain yourself and not know how to iterate through tables.

My other option for you is a generic for loop:

for iteration, value in pairs(banned) do
end

value will automatically refer to the value it hits upon iterating through the given set of data returned by pairs(). This means you can just do player.Name == value rather than doing player.Name == banned[i], which is somewhat easier.

This works fine too and has less lines.

local banned = {"CodeWon","Code1Won"} --List of players that are banned

game.Players.PlayerAdded:Connect(function(player)
	for _, v in pairs(banned) do
		if v == player.Name then 
			player:Kick("You are banned")
		end
	end
end)

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