Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How come my 'GetTime' code keeps errorring when attempting to use?

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

1 answer

✓ Accepted answer

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

Computing Time

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) )

7 comments

1

But, line 13 is supposed to add the extra zero, preventing '10:9:8' from occurring. Also, I didn't know you could do it like that [For the last code]. o_e

1

Oh, I missed that, whoops, let me look more into this. Found it.

0

I just switched line 8 from 'pos = secs:sub(i+1,#secs)' to 'pos = secs:sub(i+1,i+2)', and it is working now for some reason, why is this when the other one was [probably] the same?

0

Oh, I don't think you're computing secs at all correctly, that's why.

0

Hmm, so 'secs' has to be two [At Max] numbers for the 'TimeOfDay' property to accept it? o_e

1

It has to be a two digit number. But you are selecting something after the decimal point, which isn't right. The correct math is posted here, much simpler.

0

That makes a lot of sense, and shortens code a log better than I knew, thanks a lot man! :D Also, congrats on 1.5K+! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.