Help guys, My ban script works, and it bans players fine, but the Unban is not working.
local function banPlayer(targetPlayer, reason)
targetPlayer:Kick(reason)
BanDataStore:SetAsync(targetPlayer.UserId, true)
end
local function unbanPlayer(targetPlayer)
local success, isBanned = pcall(function()
return BanDataStore:GetAsync(tostring(targetPlayer.UserId))
end)
if success and isBanned then
local unbanSuccess, unbanError = pcall(function()
BanDataStore:SetAsync(tostring(targetPlayer.UserId), false)
end)
if unbanSuccess then
print("Player unbanned successfully")
else
print("Error while trying to unban player:", unbanError)
end
else
print("Player is not banned.")
end
end
BanEvent.OnServerEvent:Connect(function(player, playerName, ReasonSent)
local targetPlayer = game.Players:FindFirstChild(playerName)
if targetPlayer then
banPlayer(targetPlayer, ReasonSent)
end
end)
UnBanEvent.OnServerEvent:Connect(function(player, playerName)
local targetPlayer = game.Players:FindFirstChild(playerName)
print("UnBanEvent triggered")
if targetPlayer then
unbanPlayer(targetPlayer)
else
local offlinePlayer = game.Players:GetUserIdFromNameAsync(playerName)
if offlinePlayer then
unbanPlayer(offlinePlayer)
else
print("Invalid player name or UserId.")
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
local success, isBanned = pcall(function()
return BanDataStore:GetAsync(player.UserId)
end)
if success and isBanned then
player:Kick("You are banned from this game.")
end
end)
Probably because when you saved into the DSS of banned players, you used the Player.UserId number, and when checking if the player is banned you are looking for a string not a number of the .UserId:
Do this on the ban function and would be better if you wrap it on a pcall and not just running it without it:
local function banPlayer(targetPlayer, reason)
targetPlayer:Kick(reason)
BanDataStore:SetAsync(tostring(targetPlayer.UserId), true)
end
Btw, there are more issues on the code, for example when you check when player joins, you are checking a number value, but you know that your DSS is using strings, so:
game.Players.PlayerAdded:Connect(function(player)
local success, isBanned = pcall(function()
return BanDataStore:GetAsync(tostring(player.UserId)) -- turn into string
end)
if success and isBanned then
player:Kick("You are banned from this game.")
end
end)
Important to perform retries when DSservice fails on this part:
local function unbanPlayer(targetPlayer)
local success, isBanned = pcall(function()
return BanDataStore:GetAsync(tostring(targetPlayer.UserId))
end)
if success then
warn("success check")
if isBanned then
warn("proceed to unban, banned player")
local unbanSuccess, unbanError = pcall(function()
BanDataStore:SetAsync(tostring(targetPlayer.UserId), false)
end)
if unbanSuccess then
print("Player unbanned successfully")
else
print("Error while trying to unban player:", unbanError)
end
else
warn("player is not banned")
end
else
warn("issue on DSS check")
warn("PERFORM RETRIES!")
-- create a function to perform retries
end
end
And same when the player joins, do retries if DSS fails
Your unban function is overcomplicating how you should unban someone. You can just use :RemoveAsync.
Code:
local Players = game:GetService("Players")
local function banPlayer(targetPlayer, reason)
targetPlayer:Kick(reason)
BanDataStore:SetAsync(targetPlayer.UserId, true)
end
local function unbanPlayer(targetPlayer)
if type(targetPlayer) == "number" then
BanDataStore:RemoveAsync(targetPlayer)
else
BanDataStore:RemoveAsync(targetPlayer.UserId)
end
end
BanEvent.OnServerEvent:Connect(function(player, playerName, ReasonSent)
local targetPlayer = Players:FindFirstChild(playerName)
if targetPlayer then
banPlayer(targetPlayer, ReasonSent)
end
end)
UnBanEvent.OnServerEvent:Connect(function(player, playerName)
local targetPlayer = Players:FindFirstChild(playerName)
print("UnBanEvent triggered")
if targetPlayer then
unbanPlayer(targetPlayer)
else
local offlinePlayer = Players:GetUserIdFromNameAsync(playerName)
if offlinePlayer then
unbanPlayer(offlinePlayer)
else
print("Invalid player name or UserId.")
end
end
end)
Players.PlayerAdded:Connect(function(player)
local success, isBanned = pcall(function()
return BanDataStore:GetAsync(player.UserId)
end)
if success and isBanned then
player:Kick("You are banned from this game.")
end
end)