Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isn't my countdown script working, and instead displaying a static number?

The aim here is to make a countdown that displays itself on the players GUI. for that reason this script is in StarterGui -> ScreenGui -> TextLabel

local CurrentTime = os.time()
local EndTime = CurrentTime + 60 -- 60 seconds past CurrentTime
local TimeLeft = EndTime - CurrentTime -- Time remaining on the clock
Player = game.Players.LocalPlayer --for adding gamepass later on, ignore
local MarketplaceService = game:GetService("MarketplaceService") --for adding gamepass later on, ignore

while true do -- while loop
	if TimeLeft == 0 or os.clock == EndTime then --end of countdown
		script.Parent.Text = "End of countdown." -- script.Parent is a TextLabel
	else
		script.Parent.Text = tostring(TimeLeft) -- script.Parent is a TextLabel
		local TimeLeft = EndTime - os.time
	end
end

Instead of displaying the correct amount of time, counting down in seconds, it displays 60

1 answer

I exchanged the while true loop for a for loop, and the resulting loop looks like that

Player = game.Players.LocalPlayer --for adding gamepass later on, ignore
local MarketplaceService = game:GetService("MarketplaceService") --for adding gamepass later on, ignore

for i = 60, 0, -1 do
	script.Parent.Text = string.format("Countdown ends in %i seconds.", i)
	wait(1)
	if i == 0 then
		script.Parent.Text = "countdown ended!"
		break
	end
end

2 comments

0

I replaced break with i = 60 because I need this countdown to loop itself multiple times

0

put [SOLVED] in your question title since you already solved it

This discussion is preserved as it was. New answers, comments, and votes are closed.