Data Store Countdown

Does anyone know how to make a countdown that counts down even when a server is not on? I am trying to make a Jail System with DataStore and I want players with the ability to jail, to be able to set time (Months, Days, Minutes) and I want it the timer to countdown even if they are not in-game or they are not in-game.

3 Likes

You could log the date and time when the player leaves the game, and save it in a datastore.

When said player rejoins, subtract the old date and time from the current date and time, and that will give you the amount of time that has passed. You can then continue your logic accordingly.

8 Likes

Even better, log the time when the player will no longer be in jail (current time + time in jail). Then compare that number to the current time to see if they are out yet.

12 Likes

It’s been a loooong time since I’ve figured this out but yes ok so for any possible people that have looked out for this type of things simply use os.time() with a DataStore like:

Server script

local DS = game:GetService("DataStoreService")
REMOTE_EVENT.OnServerEvent:Connect(function(plr, time) --Some RemoteEvent
     local Data = DS:GetDataStore("JailData_1", plr.UserId)
     Data:SetAsync("TimeLeft", time)
end)

P.PlayerAdded:Connect(function(plr)
     local Data = DS:GetDataStore("JailData_1", plr.UserId)
     local TimeLeft = tonumber(Data:GetAsync("TimeLeft"))
     if TimeLeft == nil then
         Data:SetAsync("TimeLeft", 0)
     else
         if ( TimeLeft > 0 and TimeLeft - os.time() > 0 ) then
             --Jail stuff
         end
     end
end)
5 Likes