Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Loot_O 42
4 years ago

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.

0
Also, I used a script to make this. Loot_O 42 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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

0
may want to use .Changed ForeverBrown 356 — 4y
0
No need there. If you can check on a loop it just makes it easier MrCatDoggo 213 — 4y
Ad
Log in to vote
0
Answered by
Azuc 112
4 years ago
Edited 4 years ago

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
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. Loot_O 42 — 4y

Answer this question