Code issues - Time Stop when player reset bug

Hello! I have a small bug in my code and I’m not sure how to fix it, any ideas?

The problem I’m having is that when the player reset, the time stops, but it should continue regardless of whether the player resets. Should I handle it on the server or how can I resolve it?

what i want
I would like the time to be the same for everyone and always be continuously advancing.

Currently, the LocalScript is located at:

image

Localscript:

-- Get references to objects
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local screenGui = playerGui:WaitForChild("Game_UI")
local textLabel = screenGui:WaitForChild("Timer_UI_CurrentTime")

-- Set the clock speed
local clockSpeed = 1 

while true do
	wait(1) -- Wait for one second

	local currentTime = game.Lighting.ClockTime
	local newTime = currentTime + (1 * clockSpeed) / 60 -- Increase time by 1 minute

	game.Lighting.ClockTime = newTime

	local hour = math.floor(newTime)
	local minute = math.floor((newTime % 1) * 60)

	-- Format the time to be displayed as "hh:mm"
	local formattedTime = string.format("%02d:%02d", hour, minute)

	-- Update the TextLabel text
	textLabel.Text = "Time: " .. formattedTime
end

Find the ScreenGui where your timer is and set ResetOnSpawn to false.

You should also be using task.wait instead of wait for this reason.

1 Like

Thank you so much for the tip, I had no idea about that, and it was very helpful. Thanks! :smiley:

1 Like

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