I want to have a day-night cycle where the day is 2 minutes and the night is 2 minutes as well.
The issue is that the script seems to not function properly and the day is a lot less than 2 minutes and the night is a lot more than 2 minutes.
Here’s the script.
local dayLength = 2
local cycleTime = dayLength*2
local minutesInADay = 2*2
local lighting = game:GetService("Lighting")
local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
if dayLength == 1 then
dayLength = 2
end
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
until false
local dayLength = 2
local cycleTime = dayLength*2
local minutesInADay = 2*2
local lighting = game:GetService("Lighting")
local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
if dayLength == 1 then
dayLength = 2
end
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(0.1) -- wait time of 1/15 seconds was very short and it was causing the issue.
until false
Also time goes backwards. Here’s is output of printing time of day after every update:
Wait so if suppose I want the daytime to be 2 minutes and night time to be 10 minutes what would the whole script look like (sorry for such a stupid question just really needed the answer to it)
A script for updating time without stopping could be something like this:
local lighting = game:GetService("Lighting")
while wait(x) do -- replace x with a number to wait x seconds before the next step, x number decides about amount of time to wait before the next time update
lighting:SetMinutesAfterMidnight(lighting:GetMinutesAfterMidnight() + y) -- replace y with amount of time to add to current time
print(lighting.TimeOfDay) -- printing TimeOfDay might help with finding good numbers for x and y
end
I actually wanna know what values I have to change to make the day 2 minutes and the night 10 minutes in this script in this script (I’m extremely sorry that you’re being asked so many questions I’m kinda new to scripting
local dayLength = 2
local cycleTime = dayLength*2
local minutesInADay = 2*2
local lighting = game:GetService("Lighting")
local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
if dayLength == 1 then
dayLength = 2
end
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(0.1)
until false
This works fine after fixing small errors, example: local cycleTime = dayLength12, but it’s supposed to be local cycleTime = dayLength. As I said it works.
-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 12
local cycleTime = dayLength*12
local minutesInADay = 24*12
local lighting = game:GetService("Lighting")
local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
if dayLength == 0 then
dayLength = 1
end
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
until false
I actually wanna know what values I have to change to make the day 2 minutes and the night 10 minutes in this script in this script (I’m extremely sorry that you’re being asked so many questions I’m kinda new to scripting
local dayLength = 2
local cycleTime = dayLength*2
local minutesInADay = 2*2
local lighting = game:GetService("Lighting")
local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
if dayLength == 1 then
dayLength = 2
end
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
I don’t need a cycle for never ending game this cycle is only gonna run once but I want to know where the night time value is placed and where the day time value is placed.
This code has already configured that the day lasts 2 minutes and the night 10
-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 2
local cycleTime = dayLength*10
local minutesInADay = 2*10
local lighting = game:GetService("Lighting")
local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
if dayLength == 0 then
dayLength = 1
end
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
until false
This code has already configured that the day lasts 2 minutes and the night 10
Fix bugs
-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 2
local cycleTime = dayLength*10
local minutesInADay = 2*10
local lighting = game:GetService("Lighting")
local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
if dayLength == 0 then
dayLength = 1
end
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
until false