local minutesAfterMidnight = 0
while true do game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight) minutesAfterMidnight = minutesAfterMidnight + 1 wait(1)
end
Why do you have to use local and a variable to do the night/day cycle?
local minutesAfterMidnight = 0
while true do game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight) minutesAfterMidnight = minutesAfterMidnight + 1 wait(1)
end
Why do you have to use local and a variable to do the night/day cycle?
Here is your code,
local minutesAfterMidnight = 0
while true do
game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
minutesAfterMidnight = minutesAfterMidnight + 1
wait(1)
end
There is your script, but how does it work?
This script isn't too complicated. First, the script makes a variable. A variable represents a value. For example,
local x = 5
print(x)
In the above, we set x equal to 5. So when we print 5, it prints 5.
The simple answer is, we don't.
Local variables are basically the same as regular variables, but they can't be used outside code. I don't want to go into to, but a good rule of thumb is to always use Local Variables.
After we name something, we don't have to define it again. So we only need to put local in front of new variables.
Example,
local x = "Hello"
print(x)
x = "Goodbye"
print(x)
Loops just run code multiple times. A while loop runs until the value it's looking for is false or nil. Example,
while false do
print("Won't print")
end
while true do
print("will print forever")
wait()
end
So we have a loop that runs forever, and a variable. If you'll notice, the variable get bigger each time the loop runs, because we're adding 1 to it each time.
What the function SetMinutesAfterMidnight does it it sets the current time to the value, and sets that value to the time in game minutes after midnight.
There's also a function called :GetMinutesAfterMidnight that we can use.
Example,
while wait(.2) do
game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight()+1)
end
It Sets the number of minutes after midnight to the number of minutes after midnight plus 1.
It's just another way of writing the loop to have a built in delay.
Good Luck!
Let's break down the code to analyse each block of code.
local minutesAfterMidnight = 0
You don't need to use the local scope in front of the variable, as any local variables outside of a function would play as a global variable would. We set the variable to 0, because 0/60 is 0:00:00, meaning that the cycle would start from midnight going up.
while true do
game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
minutesAfterMidnight = minutesAfterMidnight + 1
wait(1)
end
The "while true do" is a continuous loop that runs forever. This loop can easily break a game, or induce a heap amount of lag, due to the servers continuously running the loop. Inside the loop sits "game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)". This finds the hierarchical format of Lighting from the game, and finds the class ":SetMinutesAfterMidnight()" that is linked with Lighting. The ":SetMinutesAfterMidnight()" class sets the amount of time (in minutes) that have passed since midnight. Because it's set in minutes, if you want the time to pass an hour after midnight, you would put ":SetMinutesAfterMidnight(60)".
The next part of the code is the "minutesAfterMidnight = minutesAfterMidnight + 1" section. This is known as an increment. Because the loop is continuously running, whatever is inside will continually be called and acted upon. "minutesAfterMidnight = minutesAfterMidnight + 1" would essentially find the current value of the variable "minutesAfterMidnight", and add one each time the loop repeats. As such, it will continuously add one to the variable.
Lastly is the "wait(1)" function, which essentially waits one second before repeating the loop. This helps to reduce lag, or cause the script to break.
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
:3
:( You beat me to it