So I attempted to make a 3D Sun and Moon that follows the 2D Sun in Moon but it clearly doesn’t work as you can see in the video. It’s a local script in StarterPlayerScripts.
Models:
Time Script (ServerScriptService):
-- Get necessary services
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")
-- Define the duration of a full day and night cycle in seconds
local Duration = 2400 -- day and night duration (in seconds)
-- Initialize the total time variable
local totalTime = 24 * 44
-- Function to update the time of day
local function Update(deltaTime)
-- Update total time, ensuring it wraps around after reaching the duration
totalTime = totalTime % Duration + deltaTime
-- Set the ClockTime based on the total time elapsed
Lighting.ClockTime = 24 * (totalTime / Duration)
end
-- Connect the Update function to the Heartbeat event to update every frame
RunService.Heartbeat:Connect(Update)
3D Sun and Moon Script (StarterPlayerScript):
-- Get necessary services
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")
-- Define the duration of a full day and night cycle in seconds
local Duration = 2400 -- day and night duration (in seconds)
-- Initialize the total time variable
local totalTime = 24 * 44
-- Celestial body models
local SOLAR_DIST = 100
local sun = workspace:WaitForChild("Sun")
local moon = workspace:WaitForChild("Moon")
-- Function to update the time of day
local function UpdateTime(deltaTime)
-- Update total time, ensuring it wraps around after reaching the duration
totalTime = totalTime % Duration + deltaTime
-- Set the ClockTime based on the total time elapsed
Lighting.ClockTime = 24 * (totalTime / Duration)
end
-- Function to update sun and moon positions
local function UpdateSunAndMoon()
local sDir = Lighting:GetSunDirection()
local mDir = Lighting:GetMoonDirection()
local origin = CFrame.new(workspace.CurrentCamera.CFrame.Position)
sun:PivotTo(origin + SOLAR_DIST * -sDir) -- Negate if needed
moon:PivotTo(origin + SOLAR_DIST * -mDir) -- Negate if needed
end
-- Combine both updates in a single function
RunService.Heartbeat:Connect(function(deltaTime)
UpdateTime(deltaTime) -- Update the time of day
UpdateSunAndMoon() -- Update the positions of the sun and moon
end)
Video:
I’m not a good scripter by, any means, I’m just trying to make something cool and have no idea what I’m doing so please, help.
The Solar Dist is meant to be further but the Sun is under the map so I lowered it so you can see it clearly.