I am trying to make a timed ban script and I’m not sure how to go through with it to ensure it works correctly. Here’s my script right now
local BannedPlayers = {} -- Table to store all banned players.
local players = game:GetService("Players")
local BanReason = script.Parent.Parent.BanReason
local PlayerUserNameBox = script.Parent.Parent.Parent.PlayerManagementFrame.PlayerUsername
local BanRemote = game:GetService("ReplicatedStorage").BanRemote
BanRemote.OnServerEvent:Connect(function(plr, TargetPlayer, OfficialBanReason, BanDuration)
local daysToBeBanned = BanDuration * 86400
if plr then
local playerToBan = game.Players:WaitForChild(TargetPlayer)
if playerToBan then
playerToBan:Kick(OfficialBanReason)
wait(daysToBeBanned)
end
end
end)
players.PlayerAdded:Connect(function(bannedplayer)
if BannedPlayers[bannedplayer] then
bannedplayer:Kick("You are banned from U.C.G.")
end
end)
What I want is to have some sort of timer that continues to count while the banned player isn’t in game, but only count for that player. I have local daysToBeBanned = BanDuration * 86400 to tell the script how many seconds to wait. (there’s a private UI that has a text box, which asks you to type the amount of days to ban that specific player e.g., “1” = 1 day)
when the player is banned you should save this in a ban datastore with the player userid being the key, and the value being the unix timestamp of when their ban ends. When a player joins, you should lookup their userid in the datastore, and check if they have a ban end time, and if the current unix time is < the end time, kick them from the server.
local BanDS = game:GetService("DataStoreService"):GetDataStore("BannedPlayers")
local players = game:GetService("Players")
local BanReason = script.Parent.Parent.BanReason
local PlayerUserNameBox = script.Parent.Parent.Parent.PlayerManagementFrame.PlayerUsername
local BanRemote = game:GetService("ReplicatedStorage").BanRemote
BanRemote.OnServerEvent:Connect(function(plr, TargetPlayer, OfficialBanReason, BanDuration)
local secondsToBeBanned = BanDuration * 86400
if plr then
local playerToBan = game.Players:WaitForChild(TargetPlayer)
if playerToBan then
BanDS:SetAsync(playerToBan.UserId,os.time()+secondsToBeBanned)
playerToBan:Kick(OfficialBanReason)
end
end
end)
players.PlayerAdded:Connect(function(plr)
local val = BanDS:GetAsync(plr.UserId)
if val and os.time() < val then
plr:Kick("still banned")
end
end)
Okay. I just tried this, I’m getting no errors, but It’s just doing a normal kick and they player is able to join back. Here’s me Local Script:
local players = game:GetService("Players")
local player = players.LocalPlayer
local BanReason = script.Parent.Parent.BanReason
local BanDuration = script.Parent.Parent.BanDuration
local PlayerUserNameBox = script.Parent.Parent.Parent.PlayerManagementFrame.PlayerUsername
local BanRemote = game:GetService("ReplicatedStorage").BanRemote
script.Parent.MouseButton1Up:Connect(function()
local EnteredUsername = PlayerUserNameBox.text
local TargetPlayer = game.Players:FindFirstChild(EnteredUsername)
print("Entered username to ban is: " .. TargetPlayer.Name)
local OfficialBanReason = BanReason.Text
local OfficialBanDuration = BanDuration.Text
BanRemote:FireServer(TargetPlayer, OfficialBanReason, OfficialBanDuration)
print(player.Name .. " Fired BanRemote")
end)
and here’s my server script:
local players = game:GetService("Players")
local BanRemote = game:GetService("ReplicatedStorage").BanRemote
BanRemote.OnServerEvent:Connect(function(plr, TargetPlayer, OfficialBanReason, OfficialBanDuration)
if plr then
local playerToban = players:GetUserIdFromNameAsync(TargetPlayer.Name)
print("Player targetted to ban is: " .. TargetPlayer.Name .. "(" .. playerToban .. ")")
local DurationforBan = tonumber(OfficialBanDuration)
print("Player's ban duration is: " .. DurationforBan)
players:BanAsync({
UserIds = {playerToban},
Duration = DurationforBan,
DisplayReason = OfficialBanReason,
PrivateReason = OfficialBanReason
})
print("Banned: " .. TargetPlayer.Name)
end
end)
you can’t update the ban message to show the time that’s left on a ban, roblox automatically shows the time that’s left. the only thing you can do is show the initial ban time which isn’t ideal because they can join a few days later and find out they’re banned and the # of days wouldn’t have decreased