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

Need help having this code explained as to how it DOES work properly without any errors?

Asked by 8 years ago

So I have been following some of the scripting tutorials on the Roblox wiki and need some better explanation to this code.

So basically I have made a day/night cycle which I completely understand how that works and everything in that department is completely fine, but here is the code for it anyways:

minutesAfterMidnight = 0
while true do
    wait(.1)
    game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
    minutesAfterMidnight = minutesAfterMidnight + 10
    print(minutesAfterMidnight)
end

Yes I know this cycles through days and nights very quickly, but that is for testing purposes.

Now here is the part I do not understand; I have made a street light type-thing that turns itself off and back on depending on the time. Here is the code for it:

lightPart = script.Parent
pointLight = lightPart.PointLight
while true do
    wait(.1)
    if game.Lighting:GetMinutesAfterMidnight() > 60 * 6 then -- 6 am turn lamp off
        lightPart.Material = "Plastic"
        pointLight.Enabled = false
        if game.Lighting:GetMinutesAfterMidnight() > 60 * 18 then -- 6 pm turn lamp on
            lightPart.Material = "Neon"
            pointLight.Enabled = true
        end
    end
end

I would like to say that I did all of this without even looking at the tutorial for probably a couple days (I do this to challenge myself), but I realize that I don't think this script should work, but it does.

The reason I don't understand why this code DOES work is just because I'm checking the specific amount of minutes past midnight. Basically my day/night cycle will make it be thousands upon thousands of minutes past midnight, yet the script still works just fine. Shouldn't it just stay turned on because the minutes after midnight is indeed far greater than 60*18?

There is one more part I do not understand, which is why don't I need to check and make sure the minutes after midnight is also less than a value. Like make sure minutes after midnight is greater than 6 am but ALSO less than 6 pm?

Also I'm not looking for answers like, "That's just how it works."

I hope I didn't make this too long. I hope someone can explain to me these things.

Anyways, thank you for reading.

1 answer

Log in to vote
0
Answered by
cc567 50
8 years ago
Edited 8 years ago

For your first question, this code

if game.Lighting:GetMinutesAfterMidnight() > 60 * 6 then --this is saying 6 times 60 which is 360 minutes from midnight.  This is 6 am
if game.Lighting:GetMinutesAfterMidnight() > 60 * 18 then -- this is saying 18 times 60 which is 1080 minutes after midnight.  This is 6pm

For your second question,

The space between 6am and 6pm is daytime so you want your light disabled during this time and enabled after 6pm and before 6am. You don't need to specify this since greater than 6am is equal to less than 6pm. You might notice that

GetMinutesAfterMidnight()>60*6

Can function just like

GetMinutesAfterMidnight()==60*6

when making a streetlamp. Hope this helps.

Ad

Answer this question