Timer Script Doesn't Work, Need Help Fixing it

I made a round-based survival game, and I made for it a script that displays a timer on the top of the screen.

TotalSeconds = game.StarterGui.TopText.Time:GetAttribute('TotalSeconds')
repeat
	local Minutes = math.floor(TotalSeconds / 60)
	--/Seconds Function
	if (TotalSeconds % 60) < 10 then
		Seconds = (0 .. TotalSeconds % 60)
	else
		Seconds = (TotalSeconds % 60)
	end
	--Seconds Function end
	game.StarterGui.TopText.Time.Text = string.format(Minutes .. ':' .. Seconds)
	TotalSeconds = TotalSeconds - 1
	wait(1)
until TotalSeconds < 0

When I run the game in Studio, it works properly:
robloxapp-20210813-1708178.wmv (161.6 KB)
but when I play then game (studio and player) its breaks, requiring me to reset for the time to update.
robloxapp-20210813-1708359.wmv (3.3 MB)

I am trying to make the script work ingame. If anybody can, please try to solve it. Thank you!

1 Like

Can you post the script which updates the timer.

I posted the script. It’s in the initial post.

to access the players GUI you have to use game.Players.LocalPlayer.PlayerGui this is why the script only works on the server and not on the client:

so for your script to work on the client you just have to change it to:

local playerGui = game.Players.LocalPlayer.PlayerGui
local TotalSeconds = playerGui:WaitForChild("TopText"):WaitForChild("Time"):GetAttribute('TotalSeconds')

repeat
	local Minutes = math.floor(TotalSeconds / 60)
	--/Seconds Function
	if (TotalSeconds % 60) < 10 then
		Seconds = (0 .. TotalSeconds % 60)
	else
		Seconds = (TotalSeconds % 60)
	end
	--Seconds Function end
	playerGui.TopText.Time.Text = string.format(Minutes .. ':' .. Seconds)
	TotalSeconds = TotalSeconds - 1
	wait(1)
until TotalSeconds < 0

It errors: Workspace.TimerTest:1: attempt to index nil with 'PlayerGui’
Is this supposed to be a local script?

Yeah thats what I said

So it just has to be a local script in starter player scripts

1 Like

Alright it works! Thank you for your time and knowledge. :wink: