I have an Airline game in Roblox and i would like to have the TimeOfDay the same as the CEST timezone. I have seen this in other games but im not sure how to implement it.
I have found videos and other topics about this, but it was only to get the local time of the player to display on a textlabel.
In order to set the game world time to be that of a timezone, you’ll have to use an external API to request the time for a given timezone, since roblox servers do not natively support that.
I would recommend making an HTTP Request to http://worldtimeapi.org/.
Before we begin, make sure that Allow HTTP Requests is on by going to Game settings > Security > Allow HTTP Requests.
local httpService = game:GetService("HttpService")
local lighting = game:GetService("Lighting")
local URL = "http://worldtimeapi.org/api/timezone/Europe/Amsterdam"
local response
-- make sure nothing breaks if this returns an error.
pcall(function()
response = httpService:GetAsync(URL)
end)
response will return a string with values for the timezone we called. A part of the string contains the unix time which we can use to get the time in that timezone.
-- grab "unixtime" from the string and turn it into a number.
function getUnixTime(str)
local unixtime = tonumber(str:match('"unixtime":(%d+)'))
return unixtime
end
then, we can turn the raw unixtime into a HH:MM:SS format, so we can use it with Lighting.TimeOfDay
function unixToTime(unixtime)
return os.date("%H:%M:%S", unixtime)
end
local unixTime = getUnixTime(response)
local TimeOfDay = unixToTime(unixTime)
local httpService = game:GetService("HttpService")
local lighting = game:GetService("Lighting")
local URL = "http://worldtimeapi.org/api/timezone/Europe/Amsterdam"
local response
local data
-- 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
Getting the weather is the same principle, find an API (preferably free) to fetch weather information from.
I did some digging and found an API called weatherapi.com
Here’s the documentation for their api:
from there, we can gather that you need to make an account and then grab your API key, we will need this to actually be able to make requests. Be careful though, sites like these usually don’t like it when you send a lot of requests in a short time frame.
you can grab your API key by creating an account, then going to your account settings and checking your API key there.
looking at the documentation, we can gather that we need this link to access the current weather data:
we would then need to give this API endpoint the correct request headers. In this case, we would use a script like this to request the weather in a location:
local httpService = game:GetService("HttpService")
local KEY = 'YOUR_API_KEY'
local LOCATION = 'CITY'
local URL = `http://api.weatherapi.com/v1/current.json?key={KEY}&q={LOCATION}`
local success, msg = pcall(function()
local response = httpService:RequestAsync({
Url = URL,
Method = "POST",
}
)
if response.Success then
print(response.Body)
-- Code to run when you get a successful response
end
end)
if not success then
print('HTTP request failed ', msg)
end
we can then write a function to extract useful data, for example cloud coverage.
from the weatherapi documentation we can see that "cloud" always returns cloud coverage in %.
we can then use a function like this to get the data from the returned string:
local function getCloudCoverage(str)
return tonumber( str:match('"cloud":(%d+)') )
end
the final code would be:
local httpService = game:GetService("HttpService")
local KEY = 'YOUR_API_KEY'
local LOCATION = 'CITY'
local URL = `http://api.weatherapi.com/v1/current.json?key={KEY}&q={LOCATION}`
local function getCloudCoverage(str)
return tonumber( str:match('"cloud":(%d+)') )
end
local success, msg = pcall(function()
local response = httpService:RequestAsync({
Url = URL,
Method = "POST",
}
)
if response.Success then
local responseBody = response.Body
local cloudCoverage = getCloudCoverage(responseBody)
print(cloudCoverage) -- will return coverage in %
-- for example you can change roblox's 'Cloud' instance density to the cloud coverage value you got
end
end)
if not success then
print('HTTP request failed ', msg)
end
Using The DateTime Library, You Can Get The User’s LocalTime
However, This Will require use in a LocalScript,
IIRC Running it in a server script will get the real-time of the region the server is located in
You can use the code snipped below to do this.
Also, here is the DateTime Library, Here you can get intel on how to format local time as well as set based on area codes and timezones. DateTime | Roblox Creator Documentation
Code Snippet:
local IsLocalTime = true
local Is12HR = true
local AreaCode = "en-us"
function GetTime()
local DT = DateTime.now()
if IsLocalTime then
return string.format("%s:%s:%s",Is12HR and DT:FormatLocalTime("hh",tostring(AreaCode)) or DT:FormatLocalTime("HH",tostring(AreaCode)),DT:FormatLocalTime("MM",tostring(AreaCode)),tostring(DT:FormatLocalTime("ss",tostring(AreaCode))))
else
return string.format("%s:%s:%s",Is12HR and DT:FormatUniversalTime("hh",tostring(AreaCode)) or DT:FormatUniversalTime("HH",tostring(AreaCode)),DT:FormatUniversalTime("MM",tostring(AreaCode)),tostring(DT:FormatLocalTime("ss",tostring(AreaCode))))
end
end
task.spawn(function()
while task.wait(1) do
local DT = DateTime.now()
local TC = DT:FormatLocalTime("A",tostring(AreaCode)) -- Get PM/AM
warn(string.format("The Time Is %s %s",GetTime(),tostring(TC)))
end
end)
which will return this:
Please feel free to reply if you have any questions and/or need help,
So I did this a bit ago and ill spare you the trouble. Here is the script to do this, you’ll want to tweak it to your specific needs as my script is setup to make the game go through real time rather than a sped up cycle.
Here is the script:
script.Value.Parent = game.ReplicatedStorage
-- 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 response
local data
local done = false
secondsInFuture = 3600 * 4 -- 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
To set this up, it works with the GMT system, so my purposes I needed GMT-11:
To do this once you find your time zone you want to change the last number in this line:
“secondsInFuture = 3600 * 4 – pdt is 7 hours ahead of UTC” To the GMT number specified then depending upon if its ahead of Greenwich or behind (+or-) you change the symbol in this line:
“local PDTTime = os.date(”*t", os.time() - secondsInFuture)"