Further in-depth explanation: I have coded a 'GetTime' type code [Which I have asked how-to-do real life timing a while back] that which would be used to Manipulation the Lighting
Service's TimeOfDay
property, however, it keeps returning an error, and stopping total execution of the code.
I am confused why it keeps on error-ring, because, I thought I coded it correctly, and the Output
keeps saying the same thing every time, saying 20:51:13.735 - bad lexical cast: source type value could not be interpreted as target
20:51:13.737 - Script 'ServerScriptService.Script', Line 19
20:51:13.738 - Stack End
, however, I can not understand what it means by that. This is the code I am currently using;
function GetTime() local hour = math.floor((tick()%86400)/60/60); local min = math.floor(((tick()%86400)/60/60-hour)*60); local secs = (tick()%86400)/60/60; secs = tostring(secs); local pos = nil; for i = 1, #secs do if secs:sub(i,i) == "." then pos = secs:sub(i+1,#secs) break end end if not pos then return end if min < 10 then min = '0'..min end return hour,min,pos end repeat local Hour, Min, Secs = GetTime() game.Lighting.TimeOfDay = tostring(Hour) ..':'.. tostring(Min) .. ':' .. tostring(Secs) game:GetService("RunService").Heartbeat:wait() until nil
That weird error is caused by you giving TimeOfDay
something that it doesn't recognize as a properly-formatted timestamp.
I'm guessing it's complaining about something like 15:0:8
-- it's missing the extra zeroes you need (it wants 15:00:08
)
A solution would be to make a padLeft
function:
function padLeft( thing, by, length ) -- very general thing = tostring(thing) return tostring(by):rep( length - #thing ) .. thing end function padClock( thing ) return padLeft( thing, 0, 2 ) end ..... game.Lighting.TimeOfDay = padClock(hour) .. ":" .. padClock(min) .. ":" .. padClock(sec)
If all you need to do is set the time, though, you shouldn't be taking this approach at all.
You should just use SetMinutesAfterMidnight
:
game.Lighting:SetMinutesAfterMidnight( (tick() %86400) / 60 )
EDIT: If you print out the string, you get something like this:
0:23:38622185104423
You missed a % 60
on the secs
somewhere slash forgot to subtract out mins * 60
(equivalent options).
There are much simpler ways to compute hours, minutes, and seconds than the way you have written.
e.g.
local withinDay = tick() % (60 * 60 * 24) local withinMinute = withinDay % 60 local withinHour = withinDay % (60 * 60) local hour = math.floor( withinDay / (60 * 60) ) local minute = math.floor( withinHour / (60) ) local second = math.floor( withinMinute / (1) )