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

How to make it so that when it's night, the light turns off, but when it's day, it turns on?

Asked by
2_MMZ 1059 Moderation Voter
3 years ago

Hello! So, I've been working on a place with a friend, and we're trying to make a ceiling light that turns off at night indicating that the store is closed, but turns on when it's day indicating that the store is open. There's no errors in the output, it just does nothing. Here's my current attempt at the script:

while wait(1) do
    if game.Lighting.ClockTime == 0 then
        script.Parent.PointLight.Enabled = false
        script.Parent.BrickColor = BrickColor.new("Really black")
    else
        script.Parent.PointLight.Enabled = true
        script.Parent.BrickColor = BrickColor.new("Institutional white")
    end
end

Any answer that actually works is fine.

(It's a workspace script inside of the light part)

1 answer

Log in to vote
1
Answered by 3 years ago

Assuming you have a script that is progressing the ClockTime of the game, here is what needs to be done. The issue was that your code is running on a loop that might not pick up when time = 0 due to the wait(1) and how quickly the ClockTimeis actually ticking.

First, you can use Changed to run a function when any property of Lighting changes.

game.Lighting.Changed:Connect(function(changedProperty) --Lighting property is changed
    if changedProperty == "ClockTime" then --If ClockTime is changed, then run
        if game.Lighting.ClockTime == 0 then --Turn off light when ClockTime == 0
            script.Parent.PointLight.Enabled = false
            script.Parent.BrickColor = BrickColor.new("Really black")
        else --Turn on light if ClockTime ~= 0
            script.Parent.PointLight.Enabled = true
            script.Parent.BrickColor = BrickColor.new("Institutional white")
        end
    end
end)
0
When I change the lighting via explorer, the light stays on. Or is this something that only works in game and not studio play? 2_MMZ 1059 — 3y
0
Nevermind, I figured out that it's something that ONLY works in game. Thanks for the help! 2_MMZ 1059 — 3y
0
The reason that is happening is because when you press play in studio, everything in the explorer is local to you, meaning if you change it while playing, the rest of the server won't see it. You can either press "Run" instead and change the time and the light will work when you change the time or you can create a little script that changes the time automatically. BunnyFilms1 297 — 3y
0
Glad I could help! If you want the time to change automatically, something simple could be creating a script and putting this in it: while wait(1) do game.Lighting.ClockTime += 1 end BunnyFilms1 297 — 3y
Ad

Answer this question