My game utilizes a script that sets the games time to the Pacific Time Zone (the games based in robloxity, so I set it to pacific since roblox HQ is in California) and it doesn’t read the right time, could be almost midnight IRL and the game will show it being 6 AM. I posted the code below, what did I get wrong?
-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 1440
local cycleTime = dayLength*60
local minutesInADay = 24*60
local lighting = game:GetService("Lighting")
local startTime = 14
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
local httpService = game:GetService("HttpService")
local lighting = game:GetService("Lighting")
local URL = "https://worldtimeapi.org/api/timezone/America/Vancouver"
local response
local data
local done = false
-- make sure nothing breaks if this returns an error.
pcall(function()
response = httpService:GetAsync(URL)
end)
-- grab "unixtime" from the string and turn it into a number.
function getUnixTime(str)
local unixtime = tonumber(str:match('"unixtime":(%d+)'))
return unixtime
end
function unixToTime(unixtime)
return os.date("%H:%M:%S", unixtime)
end
local unixTime = getUnixTime(response)
local TimeOfDay = unixToTime(unixTime)
print(TimeOfDay)
lighting.TimeOfDay = TimeOfDay
done = true
print(TimeOfDay)
if dayLength == 0 then
dayLength = 1
end
repeat
if done == true then
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
end
until false
server script, located in the serverscript service, lets see here what else can i say to pad out the character minimum… Oh ya it works in studio but breaks in player if that helps.
secondsInFuture = 3600 * 7 -- pdt is 7 hours ahead of utc
--update the time every minute which is probably enough
while task.wait(60) do
--get the pdt time in a dictionary
local PDTTime = os.date("*t", os.time() + secondsInFuture)
--get total minutes and hours
local hours = PDTTime.hour
local minutes = PDTTime.min
--get the total minutes past midnight
local totalMinutesPastMidnight = (hours * 60) + minutes
--change the lighting
game.Lighting:SetMinutesAfterMidnight(totalMinutesPastMidnight)
end
So how you had it didn’t work, but the moment I took it out of the loop and paired it with my time set which accounts for the updating after the initial setting of the time, it worked.
Here is the finished code if you want to mess around with it. I intentionally am doing seconds instead of every minute so that it keeps up with real-world time. It’s a small map so I can get away with the extra load it puts on the network.
Edit 2: Its not working right, it still grabs the wrong time when in play mode.
-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 1440
local cycleTime = dayLength*60
local minutesInADay = 24*60
local lighting = game:GetService("Lighting")
local startTime = 14
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
local httpService = game:GetService("HttpService")
local lighting = game:GetService("Lighting")
local URL = "https://worldtimeapi.org/api/timezone/America/Vancouver"
local response
local data
local done = tr
secondsInFuture = 3600 * 7 -- pdt is 7 hours ahead of utc
--update the time every minute which is probably enough
local PDTTime = os.date("*t", os.time() + secondsInFuture)
--get total minutes and hours
local hours = PDTTime.hour
local minutes = PDTTime.min
--get the total minutes past midnight
local totalMinutesPastMidnight = (hours * 60) + minutes
print(totalMinutesPastMidnight)
--change the lighting
game.Lighting:SetMinutesAfterMidnight(totalMinutesPastMidnight)
done = true
if dayLength == 0 then
dayLength = 1
end
repeat
if done == true then
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
end
until false
Update: I am adding this for anyone who may be interested in doing this for their own game. The finished code that works looks like this:
local dayLength = 1440
local cycleTime = dayLength*60
local minutesInADay = 24*60
local lighting = game:GetService("Lighting")
local startTime = 14
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
local httpService = game:GetService("HttpService")
local lighting = game:GetService("Lighting")
local response
local data
local done = false
secondsInFuture = 3600 * 11 -- pdt is 7 hours ahead of UTC
--update the time every minute which is probably enough
local PDTTime = os.date("*t", os.time() - secondsInFuture)
--get total minutes and hours
local hours = PDTTime.hour
local minutes = PDTTime.min
local seconds = PDTTime.sec
print(hours,minutes,seconds)
--get the total minutes past midnight
local totalMinutesPastMidnight = (hours * 60) + minutes + seconds / 60
print(totalMinutesPastMidnight)
--change the lighting
game.Lighting:SetMinutesAfterMidnight(totalMinutesPastMidnight)
done = true
while true do
totalMinutesPastMidnight = (totalMinutesPastMidnight + wait(1/15) / 60) % dayLength
lighting:setMinutesAfterMidnight(totalMinutesPastMidnight)
end
Why am I going to this length when I could just do a simple OS time? The game is based on an old concept called Robloxia, shout out to any devs here who remember that idea of a robloxian nation. I was trying to base a timezone for it I eventually chose UTC-11 for it as geographically I am basing the game in the Pacific somewhere (I can explain if anyone wants the reason why.). So that is why I went to the trouble, I have found out some stuff for anyone who is gonna try doing this themselves.
1: Expect a difference in what you get in the studio, and what you get in the player, it accounts for different times due to how Roblox structures its server-client communications.
2: If you want a different time zone you change two areas of this system. The first is the “secondsInFuture” variable, which is where the UTC number goes, the other which is crucial for how the system calculates time is the PDTTime local variable, where it says “os. time() - secondsInFuture” you need to change the “-” to a “+” depending on whether your UTC code is ahead of UTC or behind UTC. That’s what the UTC-11 is, it’s 11 hours behind UTC.
This should help if this script ever suits anyone for the same reason I did mine.