Help With Timed Banned Script

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)

I just don’t know how to do this.

Help is greatly appreciated. Thanks.

1 Like

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)
1 Like

just use the new ban api lol

3 Likes

o i didnt even know this was a thing

1 Like

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)

please help. Thank you.

1 Like

What do the prints show in your output?

Thank you for the reply! I got an error for that change though:

1 Like

Oh yeah I didn’t realize that was already the ID. The problem is your duration is 1 (1 second)

1 Like

I want these seconds to be days. How do I go about doing so?

1 Like

1 * 86400
^ # of days

So…

local DurationforBan = tonumber(OfficialBanDuration) * 86400

1 Like

It works! but is there a way I can make the ban message say days instead of minutes?

1 Like

Roblox should automatically format it to the time they have left in the message (I think). If not, there’s no other way

2 Likes

Divide the minutes by 1440 and do math.ceil which would give you minutes to days

1 Like

Thanks for the reply! How would that be typed into the script?

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

1 Like

Oh I see. Well thank you for the heads up!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.