Hello everyone, I need some help doing the command for unban players (the code is below) what I need to do specifically is that in this part of the code:
I need to make the player that is written in the chat unbanned
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.unban ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
print("You have been unbanned by the administrator")
-- How could i unban the player here?
-- code to unban the player here:
end
end
end
Next I will give you the complete script, this is in serverscriptservice: Complete Server Script:
-- Teams Admins and functions
local DS = game:GetService("DataStoreService")
local BanStore = DS:GetDataStore("Ban")
---- ADMINS
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
-- player added code here:
if BanStore:GetAsync(Player.UserId) then
Player:Kick("You have been banned!")
end
Player.Chatted:Connect(function(message)
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.kick ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
Player2:Kick("You have been kicked by the owner.")
end
end
end
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.warn ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
print("The administrator has given you a warning")
-- code to warning here:
end
end
end
if Player.TeamColor == BrickColor.new("Lapis") 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")
-- code to ban the player here:
BanStore:SetAsync(Player2.UserId,true)
Player2:Kick()
end
end
end
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.unban ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
print("You have been unbanned by the administrator")
-- How could i unban the player here?
-- code to unban the player here:
end
end
end
end)
end)
Does anyone know how to fix this and make the player unbanned?
Either :SetAsync(id, false) and in the script that kicks them if they’re banned make sure you check if it’s true, not just if it exists, OR :RemoveAsync(id) which should completely remove their thing
Edit: If you don’t want to change anything in the code above go with the second option since your current code just checks if data exists.
-- Teams Admins and functions
local DS = game:GetService("DataStoreService")
local BanStore = DS:GetDataStore("Ban")
---- ADMINS
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
-- player added code here:
if BanStore:GetAsync(Player.UserId) then
Player:Kick("You have been banned!")
end
Player.Chatted:Connect(function(message)
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.kick ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
Player2:Kick("You have been kicked by the owner.")
end
end
end
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.warn ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
print("The administrator has given you a warning")
-- code to warning here:
end
end
end
if Player.TeamColor == BrickColor.new("Lapis") 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")
-- code to ban the player here:
BanStore:SetAsync(Player2.UserId,true)
Player2:Kick()
end
end
end
if Player.TeamColor == BrickColor.new("Lapis") then
for i,Player2 in pairs(Players:GetPlayers()) do
if message == '.unban ' .. Player2.Name then
print("The player name is ..." .. Player2.Name)
print("You have been unbanned by the administrator")
local userId = Players:GetUserIdFromNameAsync(Player)
BanStore:RemoveAsync("user_" .. userId)
end
end
end
end)
end)
Oof, I know why it’s not working. Since the player CAN’T be in the game you have to remove the pairs loop.
The code above won’t work because you saved the player’s ban key as JUST their UserId, not “user_123123”
I’m 90% this one will finally work.
if Player.TeamColor == BrickColor.new("Lapis") then
local Player2 = string.sub(message, 8) --This returns everything after ".unban "
BanStore:RemoveAsync(Players:GetUserIdFromNameAsync(Player2))
end
Edit: You can also use
local Player2 = string.split(message, " ")
BanStore:RemoveAsync(Players:GetUserIdFromNameAsync(Player2[2]))