Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

When I test my day and night light script, it doesn't work, why?

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.

1 comment

0

Also, I used a script to make this.

2 answers

✓ Accepted answer

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!!

2 comments

0

may want to use .Changed

0

No need there. If you can check on a loop it just makes it easier

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

1 comment

0

I forgot to add elseif because it was a bit late. I would do this for something else, but I like the looping one above.

This discussion is preserved as it was. New answers, comments, and votes are closed.