More efficient way of checking if the sun has changed position

Hello Everyone

At the moment I am checking to see if the position of the sun has changed by using a while function that runs every second and checks if the sun is in the same position as the second before. However I would presume this is extremally resource heavy.

Like this:

while true do
	if sun.Position ~= sunPosition then
		--Changed
	end
	
	sunPosition = sun.Position
end

Nevermind the fact that this is not how to get the suns location.

Also before people say that I should just get it every time the my script changes the time of day, this is not an option.

Interested to see if there is a better way of going about this.

This is false.


You can use GetPropertyChangedSignal on Lighting.ClockTime and Lighting.GeographicLatitude. Any values that change here changes the suns position.

1 Like

What are you using the script for? Turning lights on and off? Shadows? A sundial?
There may be a better way to do this if we knew why you are doing it.

To be honest I was never even aware of the GetPropertyChangedSignal. Don’t know how it slipped past over the years. This could have come in useful so many other times.

https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal