What do you want to achieve? Keep it simple and clear!
I am making an in-game time system and I need the time value to be displayed with 2 decimal places at all times, even when its not needed.
What is the issue? Include screenshots / videos if possible!
I do not know how to keep the 2 decimal places. For example, when the time changes to 9:40, the 0 at the end of 9:40 gets removed automatically.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I searched far and wide, and found no solution.
Simple, you don’t use decimals. You store the time as integers in seconds then format string from that instead:
local Time = 360
local TextLabel = script.Parent
while Time > 0 do
wait(1)
Time -= 1
TextLabel.Text = string.format("%c:%c", math.floor(Time / 60), Time % 60)
end
local TextLabel = script.Parent
function ChangeTime(Time)
local FullTime = 0
while Time > 0 do
wait(1)
Time -= 1
FullTime = string.format("%c:%c", math.floor(Time / 60), Time % 60)
end
return FullTime
end
print(ChangeTime(100)) --1:40
print(ChangeTime(64)) --1:04
TextLabel.Text = ChangeTime(420)
Sure, you have a variable named Time that stores the remaining game time and you have another variable named TextLabel that hold reference to the instace that script.Parent property holds, in this case it’s TextLabel instance.
Then you have a while loop that loops until Time variable is bigger than 0.
In every loop cycle code yields for a second with wait(1) then aubtracts the value Time hold by 1, then it updates the TextLabel’s Text propert using string.format(). string.format’s first argument is the string pattern with 2 %c flags in it. %c is used for converting given numbers (as integers) to string and place it inside string by replacing the %c flag with the given integer. The next 2 arguments are minutes and seconds passed as arguments to string.format to replace the 2 %c flags respectively.