How can I make a timer that will continue even if the player has left the game?

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a daily reward chest and I know how to do everything except for making the timer continue even if the player has left.

  2. What is the issue? Include screenshots / videos if possible!
    I think I need to use datastore and os.time() but I’m not very experienced with them.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried YT and Devforum.

Off the real time and not based off the player(s)

How would i be able to get real time?

You’ll never figure it out from that last post … I so don’t want to set up a datastore just to test this.
This is off the top of my head so it may be wrong. (why did I answer this)
This is a bit tricky, may be better off getting an answer from someone that is doing this.

local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("GlobalTimerData")

local startTime
local elapsedTime = 0
local saveInterval = 600  -- Save every 10 minutes (10 minutes * 60 seconds per minute)
local lastSaveTime = os.time()

-- Function to load the saved time from the DataStore
local function loadTime()
    local success, savedTime = pcall(function()
        return PlayerDataStore:GetAsync("GlobalTimer")
    end)
    if success and savedTime then
        elapsedTime = savedTime
        startTime = os.time() - elapsedTime
    else
        startTime = os.time()
    end
end

-- Function to save the elapsed time to the DataStore
local function saveTime()
    local success, error = pcall(function()
        PlayerDataStore:SetAsync("GlobalTimer", elapsedTime)
    end)
    if not success then
        warn("Error saving time: " .. error)
    end
end

-- Connect the game closing event to save the time
game:BindToClose(function()
    saveTime()
end)

-- Load the time when the game starts
loadTime()

local function updateTimer()
    while true do
        elapsedTime = os.time() - startTime
        wait(1)  -- Update timer every second

        -- Check if it's time to save
        local currentTime = os.time()
        if currentTime - lastSaveTime >= saveInterval then
            saveTime()
            lastSaveTime = currentTime
        end
    end
end

RunService.Stepped:Connect(updateTimer)

(look mom i’m using rems)

1 Like

I changed my mind and think I’ll answer with research …

timer that never stops
time counter that saves
make a timer continue

Last one looks juicy

1 Like

Ill check these out now, thanks

I already looked at the first link, it didn’t help me

I’ve read through all of these and they didn’t help me

Like, i have read through they before

You did that tutorial? … I would think that would be the one.
My script is probably right. It is covering all the bases.
Also I can see you only looked at one of them links …
Try the last one.

You will use os.time or DateTime.now().UnixTimestamp[Millis] as described earlier. These clocks run when not in the game.