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?
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
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
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
@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
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.")
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
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)
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