How do I make a Time Counter that saves?

  1. A time counter that saves when you leave the game

  2. I’m still noob at scripting so I wasn’t able to create a time counter that saves myself

  3. I wasn’t able to find anywhere something like that I was trying to make

local second = 0
local minute = 0
local hour = 0
while wait(1) do
	if second >= 60 then
		second -= 60
		minute += 1
	end
	if minute >= 60 then
		minute -= 60
		hour += 1
	end
	second += 1
	script.Parent.Text = "second: ".. second.." minute: "..minute.." hour: "..hour
end

that’s the script what I have right now

4 Likes

To save you would need to use datastores.

1 Like

Do you want the timer to continue even after the player leaves the game, or it starts again when they rejoin?

When player is in the game then it counts time and when leaves then it dosent do anything until player rejoins the game then it will continue where it stopped

1 Like

Utilize datastores and os.time() pretty easy to do

1 Like

Alright, you will need to use datastores. There are plenty of tutorials on that so I’m sure you’re fine in that area.

For the time I would recommend making it just one number: the seconds, and converting all the seconds into minutes, hours, etc. But you can also use all three numbers.

You would use a RemoteEvent (or BindableEvent depending on what type of script this is) and have the data saving script send the timer script the time left, and have it count down from there.

2 Likes

I think you use os.time() when you want the timer to continue even after the player has left.

2 Likes

Fair ill make a script for him to learn with comments and such

1 Like

depending on your statement here is the timer script feel free to get anything from the script i dont mind

added some comments for you to understand better as you are a beginner

-- Import required services and modules
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TimerDataStore = DataStoreService:GetDataStore("PlayerTimerData")

-- Set the timer interval in seconds (how frequently the timer updates)
local TimerInterval = 1

-- Variable to keep track of the timer job
local TimerJob

-- Function to save the player's timer data to the DataStore
local function SaveTimerData(player, time)
    local key = player.UserId
    local success, error = pcall(function()
        TimerDataStore:SetAsync(tostring(key), time)
    end)
    if not success then
        warn("Failed to save timer data for player " .. player.Name .. ": " .. error)
    end
end

-- Function to start the timer for a player
local function StartTimer(player)
    -- Create a new connection to the Heartbeat event
    TimerJob = game:GetService("RunService").Heartbeat:Connect(function(dt)
        local character = player.Character
        if character then
            -- Increment the time and display it in the client
            local time = (TimerDataStore:GetAsync(tostring(player.UserId)) or 0) + dt            
            SaveTimerData(player, time)
        end
    end)
end

-- Function to stop the timer for a player
local function StopTimer(player)
    if TimerJob then
        TimerJob:Disconnect()
        TimerJob = nil
    end
end

-- Handle player join and leave events
game.Players.PlayerAdded:Connect(function(player)
    local time = TimerDataStore:GetAsync(tostring(player.UserId)) or 0
    StartTimer(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
    StopTimer(player)
end)

1 Like

Do I add the counter script into this one?

1 Like

no it should do all of the work for you. You just need to know how to use datastores to check times or just use the variable for getting datatable at playeradded and add the code there

1 Like

Do I add the script to the textlabel that I’m using or into serverscriptservice? and the script has to be script on localscript right? lol so many questions sorry

1 Like

yeah its server side script i forgot to point it out sorry

1 Like