How to make a ban script?

Hi. I am working on a game (not released yet) and I want to make a script that will kick EVERYONE except for specific people but I am not so good at scripting. Please help me.

5 Likes
local Players = Game:GetService("Players")--PlayerService

for _, player in pairs(game.Players:GetChildren()) do--loop through players
if player.Name ~= "Builderman" then--check if the players name is not builderman
print("player kicked")
player:Kick()--kicks player
else
print("player was not kicked")
end 
end
3 Likes
local Immune = {} -- insert the id of the players you want to be whitelisted

game.Players.PlayerAdded:Connect(function(Player)
 local IsImmune = false
 for i, v in next, Immune do
  if Player.UserId == v then
   IsImmune = true
  end
 end
 if not IsImmune then
  Player:Kick("You're banned")
 end
end)
3 Likes

local immune = {ID, ID, ID}

game.Players.PlayerAdded:Connect(function(plr)
for i,v in pairs(immune) do
if plr.UserId == v then
print(“Immune”)
else
plr:Kick(“You are not immune”)
end
end
end)

1 Like

This doesn’t need to be complicated.

local allowed = { 1, 156, 261, 19538 } -- just example id's, put your own and the id's of specific people

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    if not table.find(allowed, player.UserId) then
        player:Kick("You are not whitelisted")
    end
end)

However you should just go to Game Settings > Permissions and whitelist your friends there unless you can’t for some specific reason.

7 Likes

Your script will not do anything as it will run when the server starts and will only check the first player that joins. As well, use :GetPlayers() as not everything in the Players service will be players.

1 Like

By ID do you mean the number of their account or the username?

1 Like

Did my script work for you? If so, mark it as the Solution!

1 Like

I will test it out a little later and will let you know. But thanks!

1 Like

Madagascar, all these solutions posted are server bans which means if the player rejoins on a new server they will be able to join. However, if you’d like to ban them permanently, you would have to store their userId in a “ban datastore”. Then, each time the player joins, you can if their userId is in the datastore and if it is, kick the player. Let me know if you need more help, here or on Discord.

3 Likes

Those aren’t really a ban scripts, he wanted to make it so only him and the people he allowed would be able to join.
That’s a good example of an actual ban script though.

1 Like

Ah I see, my apologies I didn’t read the thread correctly.

2 Likes

No need to say sorry! Your suggestion is a good method for a ban script.

1 Like

Thanks, good to know. (------)

2 Likes

It works!!! Thank you very much!! :slight_smile:

1 Like

No problem! If you need any help on anything you can simply private message me and I’ll try to help!

1 Like