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

Can someone look over my time gui script? (12 hour time loop)

See, I'm trying to make a time gui with a 12 hour loop. I'm using a textlabel for this and already have a day and night script. I've tried a bunch of different scripts. I feel like this would work, but obviously nothing is.. This is what I have so far.

while true do
 script.Parent.Text = string.sub(game.Lighting.TimeOfDay,1,5)
 wait()
if Lighting.TimeOfDay >= 13 then
        script.Parent.Text = Lighting.TimeOfDay - 12
    elseif Lighting.TimeOfDay <= 12 then
	script.Parent.Text = string.sub(game.Lighting.TimeOfDay,1,5)
	wait()
end

Help would very much be appreciated

1 answer

✓ Accepted answer

Your script was erroring because TimeOfDay was a string and you were comparing it to a number. You either compare the numbers to ClockTime, (or TimeOfDay if you converted it to a number like steamg0d said.)

Also, you need to change the time which idk if you're doing somewhere else but added it here.

changeTimeIncrement = .1-- how often you want to change the in game clock time in seconds

while wait(changeTimeIncrement) do	
	
	--update time
	local hoursPassed = 0
	local minutesPassed = 10
	game.Lighting.ClockTime = game.Lighting.ClockTime  + hoursPassed + (minutesPassed / 60)
	
	--convert to 12 hr format
	if game.Lighting.ClockTime >= 13 then
		local hour = math.floor(game.Lighting.ClockTime) 
		local minutes = math.floor((game.Lighting.ClockTime-hour)*60 + 0.5)
		local timeString = nil
		
		if(minutes >= 10)then
			timeString = hour - 12 .. ":" .. minutes
		else
			timeString = hour - 12 .. ":" .. minutes .. "0"
		end
		script.Parent.Text = timeString
		
	elseif game.Lighting.ClockTime <= 12 then
		if(game.Lighting.ClockTime < 10)then
	  		script.Parent.Text  = (string.sub(game.Lighting.TimeOfDay,2,5))
		else
			script.Parent.Text = (string.sub(game.Lighting.TimeOfDay,1,5))
		end
	end
end


5 comments

1

Actually he can use TimeOfDay and just convert the string to a number

1

thx made edit.

0

Thank you so much! It works, but one tiny issue. Instead if 10s, I want it to go by ones, if possible. Thank you!

1

sure. change minutesPassed to 1 instead of 10 and it'll make 10 minutes pass evey 0.1 seconds. if you want time to change slower, change changeTimeIncrement to a bigger number.

0

I know I'm very late, but it still doesn't work properly. Let's say it's 7:00 p.m. It'd do 7:10, :20, :30, :40, etc all the way to 7:90. Then it'd go by 1s. 

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