What I’m trying to do is to make the part glow at night. I figured out that setting the reflectance to a negative value at night makes the part glow. This script is intended to control the reflectance value based on the time of day, but it’s not working. I’d love some help.
game.Lighting.Changed:Connect(function(ClockTime)
if ClockTime > 18 or ClockTime < 6 then
script.Parent.Reflectance = -8
script.Parent.SurfaceLight.Enabled = true
end
if ClockTime < 18 or ClockTime > 6 then
script.Parent.Reflectance = 0
script.Parent.SurfaceLight.Enabled = false
end
end)
local Lighting = game:GetService("Lighting")
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local Y: number = Lighting:GetSunDirection().Y -- Sun Direcrtion it's should be from -1 into 1
if Y < 0 then -- night time should be from 0 into -1
print("NightTime")
else -- sun time should be from 0 into 1
print("SunTime")
end
end)
You are doing it wrong. When you do game.Lighting.Changed, it fires when any property of the Lighting service changes and you’re getting the ClockTime through the arguments, which actually doesn’t exist.
The first argument of the Changed event is actually the name of the property. However, it is recommended that you don’t use the Changed property.
Instead use the GetPropertyChangedSignal method.
local Lighting = game:GetService("Lighting") -- using `GetService` is a better way to access services
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
-- don't do multiple if's, instead do `elseif`
if Lighting.ClockTime > 18 or Lighting.ClockTime < 6 then
script.Parent.Reflectance = -8
script.Parent.SurfaceLight.Enabled = true
elseif ClockTime < 18 or ClockTime > 6 then
script.Parent.Reflectance = 0
script.Parent.SurfaceLight.Enabled = false
end
end)