Timer Gui only works when click run but not play

I’m trying to make a screen GUI that counts down from 100, but if I press play or play here it doesn’t work, it freezes at 100 or 99. But if I press run it does work. The script’s parent is workspace and I tried switching from LocalScript to just Script but it still doesn’t work and I don’t know why. here’s the script:

local TextOnScreen = game.StarterGui.ScreenGuiUntilSmushed.SmushText.Text
local goal = 100
local addnumm = 0

while true do 
	wait(1)
	addnumm = addnumm + 1
	TextOnScreen = TextOnScreen -1
	if addnumm == 100 then
		TextOnScreen = 100
	end
end

Also if I reset my character it goes down a bit but then it freezes again

local player = game.Players.LocalPlayer
local TextOnScreen = player.PlayerGui.ScreenGuiUntilSmushed.SmushText
local goal = 100
local addnumm = 0

while true do 
	task.wait(1)
	addnumm += 1
	TextOnScreen.Text -= 1
	if addnumm == goal then
		TextOnScreen.Text = 100
		break
	end
end

You assigned the text to a variable. Also, you are editing StarterGui, so you should use PlayerGui.

local TextOnScreen = game.StarterGui.ScreenGuiUntilSmushed.SmushText
local goal = 100
local addnumm = 0

while true do 
	task.wait(1)
	addnumm += 1
	TextOnScreen.Text -= 1
	if addnumm == goal then
		TextOnScreen.Text = 100
		break
	end
end
1 Like

do I put this in a local script, and do I keep the script in the workspace?

It seems you’re not changing the UI displayed on the CLIENT’S screen. If you want the timer to work, you should put the code in a LocalScript.

1 Like

If you are editing a GUI of a Player. You should mention PlayerGui, also if the script is a LocalScript. You should put this script in StarterGui
Also for a shorter way of using a timer, you could use for loops
Other thing is your script is using the string of the Text for a Number, instead make the string the number
ex of your script with the stuff I said

local player = game.Players.LocalPlayer
local TextOnScreen = player.PlayerGui:WaitForChild("ScreenGuiUntilSmushed").SmushText
local goal = 100
local addnumm = 0

for T =  0, goal do
	task.wait(1)
	addnumm += 1
	TextOnScreen.Text = T
	if addnumm == goal then
		TextOnScreen.Text = "100"
		end
end

If you want the opposite, from 100 to 0, just do this

local player = game.Players.LocalPlayer
local TextOnScreen = player.PlayerGui:WaitForChild("ScreenGuiUntilSmushed").SmushText
local goal = 100
local addnumm = 0

for T =  goal, 0, -1 do
	task.wait(1)
	addnumm += 1
	TextOnScreen.Text = T
	if addnumm == goal then
		TextOnScreen.Text = "0"
		end
end
1 Like

No, this would go into StarterGui. Also, @light_lanterns’s solution may be much better, but his also has some problems, so here’s a fixed version of that:

local player = game.Players.LocalPlayer
local TextOnScreen = player.PlayerGui.ScreenGuiUntilSmushed.SmushText
local goal = 100
local addnumm = 0

while true do 
	task.wait(1)
	addnumm += 1
	TextOnScreen.Text = 100 - addnumm
	addnum = addnum < goal and addnum or 0
end
1 Like

it gives me the error:

attempt to compare nil < number 

thanks for your help btw

Oh, my bad. forgot the extra m.

local player = game.Players.LocalPlayer
local TextOnScreen = player.PlayerGui.ScreenGuiUntilSmushed.SmushText
local goal = 100
local addnumm = 0

while true do 
	task.wait(1)
	addnumm += 1
	TextOnScreen.Text = 100 - addnumm
	addnumm = addnumm < goal and addnumm or 0
end

Edit: forgot another m, so recopy the script if you copied before the edit.

2 Likes

Was about to say you forgot another m just when you noticed it lol

1 Like

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