I want to store a value of 14:30:00 in a script I used the textlabel method but is there a more efficient way?
What is your goal? (what are you trying to do with the value? save it? or make like a clock out of it?)
In a variable?
local time = "14:30:00"
i want to be able to use it in another piece of code in the script
Maybe an int value in server storage maybe? I don’t really get what you mean
yes i want to know if there is a more efficient way to make it a variable so i dont have to do tonumber()
make it a local variable, because then you can use it within the entire script
You can store hour and minute in separate variables and just concatenate them with a “:” …
local hour = 14
local minute = 30
local time = hour .. ":" .. minute
if i do a red line appears under
say local value = 14:30:00
replace value
with whatever the name of your value is.
or go with what @ComplicatedParadigm said, and do:
local hour = 14
local minute = 30
local time = hour ... ":" ... minute
You could use functions to encode
and decode
text to seconds, and the opposite:
local value = "02:30:00"
--converts text like the one shown above, to seconds
function toSeconds(val)
local split = val:split(":")
local hoursToSeconds = split[1]*3600
local minutesToSeconds = split[2]*60
local seconds = split[3]
return hoursToSeconds+minutesToSeconds+seconds
end
--formatting function(so zeros don't get removed, for example 02:04:00)
function f(val)
return string.format("%02i", val)
end
--receives and text, and converts it to seconds
function toText(val)
local hours = math.floor(val/3600)
val -= hours*3600
local minutes = math.floor(val/60)
val -= minutes*60
local seconds = math.floor(val)
return f(hours)..":"..f(minutes)..":"..f(seconds)
end
local seconds = toSeconds(value)
print(seconds)
local text = toText(seconds)
print(text)
PS: the code works when it’s tested for game.Lighting.TimeOfDay