Ban script not working as it is supposed to

Hi out there i am learning to script and created a ban script that would ban players in the table and its showing in the output that the player is in the banlist but still its not kicking me out of the game.
Please help

MAIN SCRIPT

local GetList = game.ReplicatedStorage:WaitForChild("GetList")  
game.Players.PlayerAdded:Connect(function(player) 
	GetList:Invoke(player) 
	if IsPlayerBanned == true then 
	player:Kick ("NOOB")
	end
end) 

BAN HANDELER

local GetList = game.ReplicatedStorage:WaitForChild("GetList")  
local banList = {"heroguy1134" , "Glaunet"}
GetList.OnInvoke = function(player) 
	for i, v  in pairs (banList) do 
		if v == player.Name then  
			print ("player is in the ban list")
			return true 
		else  
			print ("player is not in the ban list") 
			return false
		end
	end
	
end

I would recommend using player ID instead of player names, like this

local BannedPlayers = { -- start of table
    0, 
    1
}

game.Players.PlayerAdded:Connect(function(plr)
    for _,banned  in pairs(BannedPlayers) do
        if banned == plr.UserId then
            plr:Kick("Hi you are banned")
        end
    end
end)
1 Like

You don’t define IsPlayerBanned anywhere, so when you do if IsPlayerBanned == true then it’s essentially if nil == true then, which means the condition will never be true.

To fix that, you need to use the boolean you return from GetList.OnInvoke, and to do that you’d do something like this:

local IsPlayerBanned = GetList:Invoke(player)

The fixed script would look something like this:

local GetList = game.ReplicatedStorage:WaitForChild("GetList")  
game.Players.PlayerAdded:Connect(function(player) 
	local IsPlayerBanned = GetList:Invoke(player) 
	if IsPlayerBanned == true then 
		player:Kick("NOOB")
	end
end) 

I do believe though that you should be using player userId instead of their names, as @Snak3GT suggested.

1 Like

Like @Snak3GT said, use the players ID instead of its username. The ID will just be an easier way to do it:

local BanList = {
2832823289,
389432894
} 

I just used random id’s

It’s not necessarily easier, however it does prevent users from bypassing it if they change their username.

True. I used a script that made a chat tag by username, and they changed their username. I would find it easier because you do not have to change the script if the person changed their name.

local GetList = game.ReplicatedStorage:WaitForChild("GetList")  
game.Players.PlayerAdded:Connect(function(player) 
	local IsPlayerBanned = GetList:Invoke(player) 
	if IsPlayerBanned == true then 
	player:Kick ("NOOB")
	end
end) ```

--You didn't define a variables, but also use userid. Cause user can change their name at all times.

--To get USERID, go to console command and type --game.Players:GetUserIdFromNameAsync('yourname')

-- If it doesn't work, I suggest you to use remote function and to call it use :InvokeServer and when remote event called, use this OnServerInoke.

That wouldn’t work, as the code is trying to index nil with GetList.
Ironically, it’s because the code isn’t defining GetList.

Edit: it’s fixed now.

1 Like