Help with ban player

Hello, I need to know how I can ban the player in my code in the part where I wrote:
– How could i ban the player here?
– code to ban the player here:

Does anyone know how I can do that part following the same code? :confused:

elseif Player.TeamColor == BrickColor.new("Slime green") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.ban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been banned by the administrator")
					-- How could i ban the player here?
					-- code to ban the player here:
				end
			end
4 Likes

I think you’d need to mess around with tables in order to make that work if you want the ban to save

You can also refer to this if it helps?

1 Like

You would need to make a table with banned players and loop through it whenever anyone joins. If that person is in the table, kick them.
You can add people to the table with table.insert

To kick a player from your game, you need to use Player:Kick(). You put the player’s name instead of Player, and in between the parentheses, you can put an optional message.

if message == '.ban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been banned by the administrator")
					Player2.Name:Kick("You have been banned by the administrator")
				end

If you want to prevent the player from joining your game again, you can use player:kick once a specific player joins

Players.PlayerAdded:Connect(function(player)
	if player == "Insert the player name you want banned from your game here" then
        -- If you have multiple players in your game, you have to make a list of them and cycle through them.
       player:Kick()
    end
end) 

If you are still having trouble with Player:Kick(), you can check out the DevHub article here.

elseif Player.TeamColor == BrickColor.new("Slime green") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.ban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been banned by the administrator")
					-- How could i ban the player here?
					-- code to ban the player here:
                    player2:Kick("You have been kicked by the administrator") -- Message (You can change however you like)
				end
			end
        end

Its just using :Kick() to kick the player. It doesn’t ban them from your game, You need to write some extra code to do that

1 Like

Firstly you need to learn DataStores. Add a value called ‘Banned’ and add a datastore to it. When the player joins, the datastore will change the ‘Ban’ value to whatever it saved! And if ‘Ban’ is true, then you kick the player

2 Likes

@PipeSader I have something that May work, as doing the method your saying, some exploits can actually bypass that command by preventing any keywords from being able to be sent

I have a working ban hammer that connects to a Datastore that bans people whenever you activate the tool by clicking it

https://gyazo.com/d4b58e249fcd19c31d656fb084e4252f

1 Like

Use something like:

table.insert(banned, plr.UserId/Name)
game.Players.PlayerAdded:Connect(function(plr))
if table.find(banned, plr.UserId/Name) then
plr:Kick("You have been banned.")
1 Like

Does this only work when the player is connected? For example, if I’m the one who bans another player and I get out of the game then the player who’s ban will be able to re-enter to the game and that’s the problem :frowning:

Script to ban:

local DS = game:GetService("DataStoreService")
local BanStore = DS:GetDataStore("Ban")

elseif Player.TeamColor == BrickColor.new("Slime green") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.ban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been banned by the administrator")
					BanStore:SetAsync(Player2.UserId,true)
                    Player2:Kick()
				end
			end 

New script:

local DS = game:GetService("DataStoreService")
local BanStore = DS:GetDataStore("Ban")

game.Players.PlayerAdded:Connect(funtion(Plr)
     if BanStore:GetAsync(Plr.UserId) then
          Plr:Kick("You have been banned!")
     end
end)
2 Likes

Well, when I made a Blacklist system I made the table and put the Player IDs in using studio, not through chat in roblox player

You could use this way for a small-scale project, wouldn’t recommend for large.

But yes, I think without DataStoreService the table would not save.

1 Like

Don’t provide scripts with no context or understanding behind it, this is just spoon feeding, no one would learn like this.

@OP, you need to use data stores to accomplish this. Read up more on developer hub.

1 Like

Great! thanks you a lot! :smiley:

a last question, to unban is it good?
BanStore:SetAsync(Player2.UserId,false)

Yes. The if statement will stop kicking the player.

@SilentsReplacement I did this because a user already said what he should learn/see.

I hadn’t thought of that, what do you think if I use a bool value? Can this work?

and what about to unban someone? :scream:

Script to ban:

local DS = game:GetService("DataStoreService")
local BanStore = DS:GetDataStore("Ban")

elseif Player.TeamColor == BrickColor.new("Slime green") then
			for i,Player2 in pairs(Players:GetPlayers()) do
				if message == '.ban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been banned by the administrator")
					BanStore:SetAsync(Player2.UserId,true)
                    Player2:Kick()
         elseif message == '.unban ' .. Player2.Name then
					print("The player name is ..." .. Player2.Name)
					print("You have been unbanned by the administrator")
					BanStore:SetAsync(Player2.UserId,false)
				end
			end 
1 Like

Thank you very much brother! :smiley:

1 Like