So I made a simple light script for day and night, but when I try this, it doesn't work. I don't see any errors or something.
Light1 = script.Parent.Light1 Light2 = script.Parent.Light2 if game.Lighting.ClockTime == 5 then Light1.Material = Enum.Material.SmoothPlastic Light2.Material = Enum.Material.SmoothPlastic Light1.PointLight.Enabled = false Light2.PointLight.Enabled = false end if game.Lighting.ClockTime == 22 then Light1.Material = Enum.Material.Neon Light2.Material = Enum.Material.Neon Light1.PointLight.Enabled = true Light2.PointLight.Enabled = true end
I'm working on a new update, so I want to make this update pretty awesome.
You need to wrap your script in a while wait() do loop or similar.
In other words like this:
while wait() do Light1 = script.Parent.Light1 Light2 = script.Parent.Light2 if game.Lighting.ClockTime == 5 then Light1.Material = Enum.Material.SmoothPlastic Light2.Material = Enum.Material.SmoothPlastic Light1.PointLight.Enabled = false Light2.PointLight.Enabled = false end if game.Lighting.ClockTime == 22 then Light1.Material = Enum.Material.Neon Light2.Material = Enum.Material.Neon Light1.PointLight.Enabled = true Light2.PointLight.Enabled = true end end
Happy coding!!
Ok so, though you could loop through forever to do this that is not as performance friendly as just detecting when the time changes using GetPropertyChangedSignal which will detect when the time changes and do whatever it is you want to be done accordingly.
Here is a fix for your code using GetPropertyChangedSignal I also changed your conditional statements from == to >=. It might be worth taking the time to set a variable for if the lights are already turned on so that each clock change isn't trying to set the materials and pointlights to what they already are.
local Light1 = script.Parent.Light1 local Light2 = script.Parent.Light2 local Lighting = game:GetService("Lighting") local function timeChanged() --Function for changing your parts if Lighting.ClockTime >= 18 then --Is it night? Light1.Material = Enum.Material.Neon Light2.Material = Enum.Material.Neon Light1.PointLight.Enabled = true Light2.PointLight.Enabled = true elseif Lighting.ClockTime >= 6 then --Is it day? Light1.Material = Enum.Material.SmoothPlastic Light2.Material = Enum.Material.SmoothPlastic Light1.PointLight.Enabled = false Light2.PointLight.Enabled = false end end Lighting:GetPropertyChangedSignal("ClockTime"):Connect(timeChanged) --Detect when the ClockTime Changes
Keep in mind for this code to work changing the slider in studio wont effect anything, you need to have a script in game manually changing your clocktime for this to be effective. I have included one below for testing the script, this one is extremely fast and if you want to use it I would advise lowering the rate of change.
while wait(0.1) do -- Change 0.1 to higher numbers to change the rate it changes the value game.Lighting.ClockTime = game.Lighting.ClockTime + 0.1 -- Change 0.1 to change the amount of value changed each time end