Sun Script Not Working Properly

So, it’s pretty self explainatory. The Sun Model is meant to track with the actual sun skybox. It works sometimes but sometimes the ray inside the model just don’t. And it’s a random amount of rays.

Script in StarterPlayerScript:

-- Services --
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")

-- Vars --
local Duration = 2400
local totalTime = 24 * 44
local SOLAR_DIST = 500
local sun = workspace:WaitForChild("Sun")
local moon = workspace:WaitForChild("Moon")
local sunDecal = sun:WaitForChild("Sun_Body"):WaitForChild("Decal")
local moonDecal = moon:WaitForChild("Decal")

local function UpdateTime(deltaTime)
	totalTime = totalTime % Duration + deltaTime
	Lighting.ClockTime = 24 * (totalTime / Duration)
end

local function UpdateSunAndMoon()
	local sDir = Lighting:GetSunDirection()
	local mDir = Lighting:GetMoonDirection()
	local origin = workspace.CurrentCamera.CFrame.Position

	sun:PivotTo(CFrame.new(SOLAR_DIST * sDir + origin))
	moon:PivotTo(CFrame.new(SOLAR_DIST * mDir + origin))

	local cameraPosition = workspace.CurrentCamera.CFrame.Position
	local sunDecalParent = sun:FindFirstChild("Sun_Body")
	if sunDecalParent then
		sunDecalParent.CFrame = CFrame.lookAt(sunDecalParent.Position, cameraPosition)
	end

	moon.CFrame = CFrame.lookAt(moon.Position, cameraPosition)
end

RunService.Heartbeat:Connect(function(deltaTime)
	UpdateTime(deltaTime)
	UpdateSunAndMoon()
end)

What it looks like:

Sun Model:
image

So the parts aren’t moving all together like you expect?

It might be a problem with moving the parts before all of the other parts replicate in. This could be either from the loading time or because of StreamingEnabled. If you have SE on make sure to have the Model set to persistent. If you have the Model set to persistent then also wait for Workspace.PersistentLoaded before using the “Sun” model so everything is loaded in.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.