UserID Game Ban Script

Hey, I’m trying to code a game ban, by ID that on player join, the script will check if the user’s ID is equal to a user ID I set. How would I do this? This is my current script:

local Players = game:GetService("Players")
 
Players.PlayerAdded:Connect(function(player)
	if player.UserId == 1035365524 then
		player:Kick("You are banned from this game! Reason: Exploiting.")
	else
		print("This user is OK!")
	end
end)

Anything I messed up in my script that I should change, please tell me as I’m horrible at this.

4 Likes

Yes, what your doing is correct. I suggest you to use tables tho.

Ok, how would I do that? I struggle with these kinds of things quite often to be honest.

Your script will work. You can also do something like:

local banList = {
     [55549140] = "Being a noob";
     [1035365524] = "Exploiting";
}

Players.PlayerAdded:Connect(function(player)
    for userId, reason in pairs (banList) do
        if player.userId == userId then
            player:Kick("You have been banned from this game for" .. reason)
        end
    end
end
9 Likes

I’ll write it down for you

local Players = game:GetService("Players")
local UserIds = {12345, 12345, 54321}

Players.PlayerAdded:Connect(function(Player)
    for _, UserId in UserIds do
        if (Player.UserId == UserId) then
            Player:Kick("You are banned.")
        end
    end
end
1 Like

Alrighty, thanks so much! This helped me a lot, I’m sure it will help someone else too! :smiley:

if banList[player.UserId] ~= nil then

Will also work given the data structure of banList, and it wouldn’t require iterating through the entire dictionary as well.

3 Likes