Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 1 year ago
Edited 1 year ago

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

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

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
0
I replaced break with i = 60 because I need this countdown to loop itself multiple times ImASquirrelYipee 15 — 1y
0
put [SOLVED] in your question title since you already solved it T3_MasterGamer 2189 — 1y
Ad

Answer this question