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

Why isn't this turning invisible?

I have it so that if the time of day is greater than 6 o'clock the brick will become visible, why isn't it working?

if script.Parent.Parent.Parent.Lighting.TimeOfDay > 06:00:00
	script.Parent.Transparency = 0
else
	script.Parent.Transparency = 1
end

please help thank you

[EDIT] is it possible to turn the time into a number, and then see if the number is greater than 060000?

2 answers

2 Problems.

  1. TimeOfDay is a string(text)
  2. if statements only run once.

This is an example:

while wait() do --Check every 0.03 seconds.
	if game:GetService("Lighting").TimeOfDay == "06:00:00" --Lighting is in game. Also, use getservice. This is what time it is visible
		script.Parent.Transparency = 0
	elseif game:GetService("Lighting").TimeOfDay = "14:00:00" --What time it turns invisible.
		script.Parent.Transparency = 1
	end
end

This is another using the changed event.

game:GetService("Lighting").TimeOfDay.Changed:connect(function(Time) --This is the most efficient.
	if Time == "06:00:00" then --Starting time
		script.Parent.Transparency = 0
	elseif Time = "14:00:00" then --Ending Time
		script.Parent.Transparency = 1
end)

2 comments

0

== is not the same as =. Nor does this do the same thing -- they wanted >. The time won't necessarily hit exactly every minute, so just == is not sufficient.

0

@Blue, it's a typo. I meant 2 "="s.

06:00:00 is not a valid Lua expression (why would it be? What would it mean)?

TimeOfDay is a string. You can compare strings:

-- Use `game` when you mean `game`
if `game.Lighting.TimeOfDay > "06:00:00" then

However, this is probably not what you want.


If you look up the Lighting sevice you can read what you can use to do this.

Note that there's a GetMinutesAfterMidnight

We can use that:

local minutes = game.Lighting:GetMinutesAfterMidnight()
if minutes > 6 * 60 then
	-- 6 AM to noon to Midnight
else
	-- Midnight to 6 AM
end

As LordDragonZord pointed out, this will only happen once.

We could put this in a while loop to make it just look constantly:

function update()
	local minutes = game.Lighting:GetMinutesAfterMidnight()
	if minutes > 6 * 60 then
		-- 6 AM to noon to Midnight
	else
		-- Midnight to 6 AM
	end
end

while wait() do
	update()
end

Or we could use the Changed event on the Lighting -- since we only need to adapt when the time has changed, we can use that fact (this probably lets ROBLOX do much less work)

function update()
	local minutes = game.Lighting:GetMinutesAfterMidnight()
	if minutes > 6 * 60 then
		-- 6 AM to noon to Midnight
	else
		-- Midnight to 6 AM
	end
end

game.Lighting.Changed:connect(update)

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