How do i detect when sunset occurs real time?

Hi, I would like to know how can I detect when sunset occurs real-time, so let’s say sunset occurs at 14:45, how can the script actively detect that? and then the next day let’s say sunset occurs on 14:42, is there a way for the script to detect the time when sunset occurs

1 Like

Use os.time().
Link: os | Roblox Creator Documentation

You need to find an API that supports this functionality, you may need to end up paying. Basically use HTTPService and the API you find to get the sunset time for a specific location.

1 Like

thank you, how can i search for these APIs? does it have to be roblox associated, sorry im not experienced with this

Like @Xyphur said, you would have to use a web API. However, even if you do the results will most likely not be accurate because you don’t have the players latitude and longitude (a lot of api’s will require this). The only thing you can do is calculate the players time zone and get a random location inside of it (preferably in the center of the timezone region). However, I’m not even sure if you can do this with just a timestamp. So you may not even be able to do this.

  1. You can get the players local time in a LocalScript using DataTime.now()
  2. Using the created datetime object, create a timestamp (string) formatted to how the api you use needs it. DateTime | Roblox Creator Documentation
  3. You can then send that information to a ServerScript using a RemoteEvent
  4. In the ServerScript, upon receiving the time from a player estimate the timezone of the player by comparing it with the server time. That’s if this is even possible though.
  5. Use HttpService | Roblox Creator Documentation to send and receive information to the web api you decide to use.

For finding a web API you can try these first:
https://sunrise-sunset.org/api

2 Likes

Thank you so much for this information! To make things easier, instead of trying to get the sunset time of each individual player, is there a way to make it so it only detects if it’s sunset time in GMT timezone?

Yes. All you would have to do is send a Get request to your chosen web API with the information for the time and/or time zone. If some of the parameters require longitude and latitude then, as previously mentioned, you can just choose a location inside the timezone.

1 Like

I humbly ask, please can you lay out the steps like you did above that I should take for this route ( to detect whenever sunset occurs at GMT timezone specifically)

Sure. Since iceland is the largest country that uses GMT time, I’ll use it for the latitude and longitude. If you want a specific latitude and longitude then you can use either https://www.latlong.net/ for finding the latitude and longitude of a location. If you want to use your latitude and longitude you can go to https://www.itilog.com/. The second link isn’t 100% accurate, but it’s close enough.

Iceland’s longitude & latitude are 65° 00’ N and 18° 00’ W

Also, I will be using this web API for this.

*Note: The web API being used is free, but, you only get 1000 free request to their api a day. However, you shouldn’t need more than this. Due to your usage, you should only be calling this once every game session. Luckily it won’t let you go over this limit on accident. If you need to use a different api I recommend the https://sunrise-sunset.org/api.

Anyways, the steps are:

  1. First, click on the web api link I provided and click the Get Free API Access button. Provide the necessary information. At the end of it will give you a developer api access key. Copy the access key and paste it in a script or something. You will need it later. If you have a Github account I strongly recommend you sign in with it.
  2. Read through the documentation on their Astronomy API Documentation page. It will provide you with the necessary information you need for accessing the astronomy information for a certain area. For the purpose of this guide though, I will still be using the latitude and longitude mentioned earlier.
  3. Using your chosen method, copy the web address given in example Get request.

The example they provided for longitude and latitude was:

https://api.ipgeolocation.io/astronomy?apiKey=API_KEY&lat=-27.4748&long=153.017

Now, a breakdown of this address. I am going to be using string formatting so that several parameters can be changed easily if needed. First, the https://api.ipgeolocation.io/ part of the address. The “https” is of course telling us that the web domain is secure. Next, the api. part of the address is the subdomain used to access the api. Then, the rest is just the main website address. Next we have /astronomy. This is directing us to the api we want to access, as the service we are using has multiple. In this case, we are using their astronomy directory though.
In this next part of the address we have the parameters. They are the information we pass along in our get request. The first one is pretty standard for a lot of web api’s, although it may be named differently. This is the api key. Good thing we copied it earlier! In the script that you put it in, paste the below code in and put the api key in the API_KEY constant.

-- Our services
local HttpService = game:GetService("HttpService")

-- Our variables
local latitude = 65
local longitude = 18

-- Our constants
local API_KEY = "your api key here"
local ASTRONOMY_QUERY_URL = "https://api.ipgeolocation.io/astronomy?apiKey=%s&lat=%s&long=%s"

This will set up all the base values we need and allow us to get new results easily if we ever change the values of the latitude or longitude variables. Now we can format ASTRONOMY_QUERY_URL whenever we are using it will an Get request like so:

local success, data = pcall(HttpService.GetAsync, HttpService, ASTRONOMY_QUERY_URL:format(API_KEY, latitude, longitude), true)

Yay! Now we have our data! But wait! It isn’t in an easily-usable format yet! We must convert the JSON table to a Lua table using HttpService:JSONDecode.

local success, data = pcall(function()
	return HttpService:JSONDecode(HttpService:GetAsync(ASTRONOMY_QUERY_URL:format(API_KEY, latitude, longitude), true))
end)

There! Much better! Now we can easily access the data returned in the table! In this case we want to access the sunset key. However, since we still aren’t sure if the request has succeeded we still need to protect against it in case it does.

if success then
	print("Sunset Time: ", data.sunset)
else
	warn(data)
end

Great! But one thing you may notice, the sunset time provided is in military time. However, this isn’t anything a little old-fashion string processing can’t fix! We can add another variable for whether or not we want to use military time and then process the if we don’t want to use military time.

local useMilitaryTime = false

if success then
	local sunsetTime = data.sunset
	
	if not useMilitaryTime then
		sunsetTime = sunsetTime:split(":")
		local add = " " .. if tonumber(sunsetTime[1]) >= 12 then "PM" else "AM"
		sunsetTime[1] %= 12
		sunsetTime = table.concat(sunsetTime, ":")..add
	end
	
	print("Sunset Time: ", sunsetTime)
else
	warn(data)
end

And that should be it! Sorry that I got all tutorial-y on you, I just love helping others and I don’t know what you know or don’t know.

1 Like