How to make a brick that temporary bans you for a specific time?

Hey I have looked on devforum and youtube for how to make a script like that I want but I can’t find anything related.

So I want to make when a player touch a part the game bans him for a specific time and even if he joins he will still banned until the ban ends, like this:
Screenshot

Can anyone help me please? thanks.

1 Like

That’s cap.

1 Like

Hello, thank you for replying.

I don’t know if thats the same thing I’m looking but I will go to check it thank you!

1 Like

Use datastores! Store a os.time() of when they were banned, and a time in seconds how long they should be banned for. Then just do the following:

if timeBannedAt <= os.time() + banTime then
      player:Kick()
end
2 Likes

Hello, that’s like what I’m looking I think it will work I’ll see if it works thanks!

1 Like

I did this script but the datastore does not work, I don’t see any error in the console but when I join to the game the player gets unbanned.

local DSS = game:GetService("DataStoreService"):GetDataStore("BanTest10534")
local BanPart = game.Workspace.banPart
local Time = 15000

BanPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Character = hit.Parent
		local Player = game.Players:GetPlayerFromCharacter(Character)
		local PlayerUserId = Player.UserId
		
		local success, errormessage = pcall(function()
			DSS:GetAsync(PlayerUserId, true)
		end)
		
		if success then
			print("Banned")
		end
		
		if os.time() <= os.time() + Time then
			Player:Kick("\n You are banned try to join in: \n"..Time.. "m")
		end
	end
end)
1 Like

i think using the memorystore would be better because it will automatically destroy the ban after some time

read more about memorystore here Memory Store

local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("ban")
local banTime = 15000

workspace.BanPart.Touched:Connect(function(touchPart)
	local character = touchPart.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player == nil then return end
	-- add the ban time to the memorystore for 15000 seconds (the memorystore will automatically delete this key after 15000 seconds)
	local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time() + banTime, banTime)
	-- kick the player
	player:Kick("\n You are banned try to join in: \n" .. banTime .. "seconds")
end)

game.Players.PlayerAdded:Connect(function(player)
	-- when the player enters the game check the memorystore to see if the player is on the ban list
	local success, value = pcall(sortedMap.GetAsync, sortedMap, player.UserId)
	-- failed roblox servers might be down we don't know if the player is banned
	if success == false then return end
	-- player is not banned we can simple exit
	if value == nil then then return end
	-- work out how long there ban has left
	local deltaTime = value - os.time()
	-- kick the player and tell them how long they have left
	player:Kick("\n You are banned try to join in: \n" .. deltaTime .. "seconds")
end)
2 Likes

Hey, thanks for replying it works perfectly but the time is not decreasing it increasing I did a small change at the bottom but the time is still increasing. Is that because I replaced the datastore with the memory store service?

local BanPart = game.Workspace.banPart
local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("BanTest51213")
local banTime = 15000

BanPart.Touched:Connect(function(touchPart)
	local character = touchPart.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player == nil then return end
	local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time(), banTime)
	player:Kick("\n You are banned try to join in: \n" .. banTime .. "seconds")
end)

game.Players.PlayerAdded:Connect(function(player)
	local success, value = pcall(sortedMap.GetAsync, sortedMap, player.UserId)
	if success == false then return end
	if value == nil then return end
	local deltaTime = os.time() - value
	if deltaTime >= os.time() - banTime then
		player:Kick("\n You are banned try to join in: \n" .. deltaTime .. "seconds")
	end
end)
1 Like

ok so if you look at my code its a little different then your

im saving os.time() + banTime into the memorystore so that will be the time the ban ends

also you dont need to do this if deltaTime >= os.time() - banTime then
because the memorystore will automatically destroy itself when the ban ends and so the value will be nil when the ban ends so there is no need to do that if check

2 Likes

Thank you so much for your reply i mean if there is any way to make it go down and not up because I want to make it from 15000s down to 0s and then it will unban the player. Like this screenshot:
image

1 Like

my code already does that

lets say os.time = 10
and lets say banTime = 10
so we are saving 20 into the memorystore
local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time() + banTime, banTime)

now lets say 5 seconds has passed so
os.time = 15
value = 20
20 - 15 = 5 that means there will be 5 seconds left until the ban ends
local deltaTime = value - os.time()
deltaTime will be 5

2 Likes