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

Script is not changing text of a Hint?

Asked by 10 years ago

I made a countdown timer script that supposedly should inserts a Hint in Workspace, counts down from T-120, then delete the Hint at T-11. But when I test it, it's stuck at T-120 and doesn't count down.

01local m = Instance.new("Hint", game.Workspace)
02n = 120
03m.Text = "Time Left : T -"..n..""
04 
05while true do
06    n = n-1
07    wait(1)
08end
09 
10if n == 11 then
11        m:remove()
12        end

1 answer

Log in to vote
3
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

Remember, computers are dumb and they only do exactly what we tell them.

On line 3, you set the Hint's text to "Time Left : T -120".

This is the only place you ever tell the computer to change the text of the Hint.

In the while loop, you change the variable n, but it is simply an integer and does not actually do anything.

On line 10 you perform a check, but that line will never run because you made an eternal loop on line 05. Even if it did run, it would only run once, and would only check if n equals 11 at the time it ran.

You do have all the right code, though. You just need to configure it correctly. I'm going to change some of the variable names to make more sense for what they're used for.

01local hint = Instance.new("Hint", game.Workspace)
02local counter = 120
03 
04while true do
05    hint.Text = "Time Left : T -"..counter
06    if counter <= 11 then
07        hint:Destroy()
08    end
09    counter = counter-1
10    wait(1)
11end

Although admittedly a for loop is usually the preferred way to do this, I decided to use the code you already wrote.

0
It works. Thanks! ChromeNine 50 — 10y
Ad

Answer this question