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

Why does it change like this?

Asked by 8 years ago
for i = 120, 0, -1 do
Time = "Time: " .. tostring(math.floor(i / 60)) .. ":" .. tostring(i % 60)
G_MSG.Value = Time
wait(1)
end

I'm going to ask once again, And i don't understand why but i need more information. So when the timer goes down to about 1:09 it goes 1:9 instead to 1:09 as i want it to be. And i searched on this wiki about string and i still dont get it. Help.

0
It does this because the numerical representation of a mathematical equation is the number itself. 0 is merely a visual placeholder we have put into effect. TinyPanda273 110 — 8y

2 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

9%60 is 9. Numbers don't arbitrarily come with a zero in front of them for no reason, so you need to put it there if you want it to be there. An easy solution to this is to write ('0'..(i%60)):sub(-2) to get only the last two digits, so the string would be

Time = "Time: " .. math.floor(i/60) .. ":" .. ("0"..(i % 60)):sub(-2)
Ad
Log in to vote
0
Answered by 8 years ago

A work-around for this:

for i = 120, 0, -1 do

local timeString = i%60

if timeString < 10 then
    timeString = "0"..timeString
else
    timeString = tostring(timeString)
end

Time = "Time: " .. tostring(math.floor(i / 60)) .. ":" .. timeString

G_MSG.Value = Time
wait(1)
end
1
Great explanation User#11440 120 — 8y

Answer this question