My smoke script isn't working

I’m trying to make smoke come from a chimney when the time of day is between two times… otherwise I want the smoke to not be visible. To accomplish this, I tried to use if statements to declare when the smoke should be activated. Here is my code:

local smoke = script.Parent.Smoke

local chimney = script.Parent

if(game.Lighting.ClockTime <= 18 and game.Lighting.ClockTime >= 8) then

smoke.Enabled = false

end

if(game.Lighting.ClockTime > 18 and game.Lighting.ClockTime < 8) then

smoke.Enabled = true

smoke.Opacity = 0.5

end

Are you using a time-of-day script that changes the time? If so, you’ll want to wrap the “if” statements in a loop to repeatedly check for the time of day:

local smoke = script.Parent.Smoke

local chimney = script.Parent

while wait(10) do --check every 10 seconds

if(game.Lighting.ClockTime <= 18 and game.Lighting.ClockTime >= 8) then

smoke.Enabled = false

end

if(game.Lighting.ClockTime > 18 and game.Lighting.ClockTime < 8) then

smoke.Enabled = true

smoke.Opacity = 0.5

end

end

Yes, I am using a time of day script elsewhere to create a day and night cycle. I forgot to loop it, maybe that’s what was happening.:sweat_smile:

Easy way out is through ternary.

local Lighting = game:GetService("Lighting")
-- Better variable assignment: reuse!
local chimney = script.Parent
local smoke = chimney.Smoke

-- No while loops please! Stay event-based!
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function ()
    smoke.Enabled = (Lighting.ClockTime <= 18 and Lighting.ClockTime >= 8)
end)
2 Likes

Why don’t you check the lowest time possible? Unless you want it to be more realistic.

local lighting = game:GetService("Lighting")

local smoke = script.Parent:WaitForChild("Smoke")
local chimney = script.Parent

while true do
    wait() -- If there's no arguments, it waits the lowest time possible.
    if(lighting.ClockTime <= 18 and lighting.ClockTime >= 8) then
        smoke.Enabled = false
    else
        -- Theoretically an `else` statement would do the same as your script
        -- for the second time check.
        smoke.Enabled = true
        smoke.Opacity = 0.5
    end
    -- Change the time maybe?
    lighting.ClockTime = lighting.ClockTime + 0.001 -- Pretty slow.
end

You shouldn’t use a loop for this kind of thing in the first place. Wherever possible, always try to keep things event-based. In the case of a time-of-day system with triggers on objects based on the time, a loop is appropriate for the day cycle but the objects should be listening only to changes in time, not starting loops of their own.

An event-based change would give the best results because the function is called each time a change is made rather than relying on waiting X seconds as well as an open slot on the task scheduler.

This doesn’t work… I think it is because you haven’t really defined whether smoke.Enabled should be on or not.

Did you actually try it? If so and it did not work, could you provide me any potential console errors that arose with the code?

Smoke.Enabled is defined by the proceeding and statement. If both values are true, the bracket evaluates to true and as such, Smoke.Enabled becomes true. If any of those arguments are false, the bracket evaluates to false and Smoke.Enabled takes that value.

1 Like

No console errors arose… however the smoke was not coming through. I’m trying to assess what could of gone wrong.

If you’d like to debug this yourself to try and see where the issue lies, you can print the result of the and statement. This can help you determine whether it’s an issue with the code (silent error?) or your smoke object itself.

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function ()
    smoke.Enabled = (Lighting.ClockTime <= 18 and Lighting.ClockTime >= 8)
    print(Lighting.ClockTime <= 18 and Lighting.ClockTime >= 8)
end)

If there’s no console errors, I’m doubtful it’s the code that’s in the wrong but it could be the interval here. The current interval is [8, 18]. You may want to try different intervals: (8, 18), (8, 18] or [8, 18).

It resulted in a “true” output twice and the rest of the time it was “false”. I think it was because you did [8, 18].

Yep. That means the code itself works and it’s enabling your smoke. Just play around with some intervals if needbe. Your code problem is solved, so now you’re left with trying to get the actual smoke object to show. This will require changes in the smoke object properties.

Try playing around with some different numbers and making sure you don’t have too many smoke objects around. There is a certain maximum to how many particles can be on the screen at one time and exceeding that limit produces finicky results for rendering.

Speaking of particles, ParticleEmitters basically superseded the old Fire, Smoke and Sparkles objects. Eventually it’d be worth your while to change to ParticleEmitters. They also offer a wider range of customisability.

I’ve tried quite a bit of numbers… none of the combinations seem to work. At first I thought it was because you had <18 and >8 but that didn’t solve the issue either.

At this point though, the root issue is resolved. Its now up to you to fix the rest of this. Play with some numbers, change the intervals and make sure the properties of the smoke object allow it to be properly seen, consider ParticleEmitters, so on. These are small things that you can change on your own time.