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

Why won't this script change the TimeOfDay property in Lighting?

This script is supposed to increase the time by one minute every second, so 1 hour in the game is 1 minute in real life. Here is the script:

--[[Details:
Parent of script is Lighting
It does not say there is an error in output
The TimeOfDay property will not change
The default value of the property is "14:00:00"]]

local Lighting = script.Parent
local hour = 14
local minute = 00

function addMinute()
	if minute == 59 then
		minute = 00
		if hour == 23 then
			hour = 00
		else
			hour = hour + 1
			Lighting.TimeOfDay = tostring(hour) .. ":" .. tostring(minute) .. ":00"
		end
		Lighting.TimeOfDay = tostring(hour) .. ":" .. tostring(minute) .. ":00"
	else
		minute = minute + 1
		Lighting.TimeOfDay = tostring(hour) .. ":" .. tostring(minute) .. ":00"
	end
end

while true do
	wait(1)
	addMinute()
end

3 comments

1

I found out the problem: the script won't run in Lighting but it does in workspace. Thanks for the advice

0

Man, you had me worried. I didn't answer because I couldn't see any problem with your code at all. Try putting that script in ServerScriptService instead of Lighting. :P

0

I tried to accept both answers but I could only accept one

2 answers

✓ Accepted answer

A much simpler way is just to add 1 every second using :SetMinutesAfterMidnight().

local lighting = game:GetService("Lighting")

while wait(1) do
	local minutes = lighting:GetMinutesAfterMidnight()
	lighting:SetMinutesAfterMidnight(minutes + 1)
	print(string.sub(lighting.TimeOfDay, 1, 5)) -- If you want to use it in a textlabel
end

tostring() is not needed, just put the variable in and it will work. Also, I'd suggest using :SetMinutesAfterMidnight() for a day/night cycle.

local Lighting = script.Parent
local hour = 14
local minute = 00

function addMinute()
    if minute == 59 then
        minute = 00
        if hour == 23 then
            hour = 00
        else
            hour = hour + 1
            Lighting.TimeOfDay = hour.. ":" ..minute.. ":00"
        end
        Lighting.TimeOfDay = hour.. ":" ..minute.. ":00"
    else
        minute = minute + 1
        Lighting.TimeOfDay = hour.. ":" ..minute.. ":00"
    end
end

while true do
    wait(1)
    addMinute()
end

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