Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't this turning invisible?

Asked by 9 years ago

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

Log in to vote
1
Answered by 9 years ago

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)
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. BlueTaslem 18071 — 9y
0
@Blue, it's a typo. I meant 2 "="s. EzraNehemiah_TF2 3552 — 9y
Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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)

Answer this question