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 9 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.

local m = Instance.new("Hint", game.Workspace)
n = 120
m.Text = "Time Left : T -"..n..""

while true do 
    n = n-1
    wait(1)
end

if n == 11 then
        m:remove()
        end

1 answer

Log in to vote
3
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 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.

local hint = Instance.new("Hint", game.Workspace)
local counter = 120

while true do 
    hint.Text = "Time Left : T -"..counter
    if counter <= 11 then
        hint:Destroy()
    end
    counter = counter-1
    wait(1)
end

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 — 9y
Ad

Answer this question