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

Simple fire script won't change actual fire size?

Asked by 8 years ago

I'm trying to make a fire get smaller and smaller periodically by using the Fire's Size property, and in the output it shows that the size is smaller and smaller but in game the fire appears to be the same size. Any thoughts?

local Campfire = script.Parent.Parent
local Fire = script.Parent.Fire
local Strength = Fire.Size

while true do
    print(Strength)
    wait(5)
    Fire.Size = Strength - 1

    if Strength == 0 then 
        Campfire:remove()
    end
end

3 answers

Log in to vote
2
Answered by 8 years ago

Your main problem is how variables behave. When you set a variable at the beginning, like you have local Strength = Fire.Size, then that variable is set once and never changes. You are using it like it is the current value of Fire.Size. This is a common early pitfall I've seen on here and the discord server.

Instead, you need to get the current value of Fire.Size and subtract 1 from that.

local Campfire = script.Parent.Parent
local Fire = script.Parent.Fire

while true do
    wait(5)
    Fire.Size = Fire.Size - 1

    if Strength <= 0 then 
        Campfire:Destroy()
    end
end

You have one other moderate problem and one other minor problem. First, :remove() is deprecated, don't use it, use :Destroy() instead. Second, you had if Strength == 0 then which might not always trigger if, for instance, later you set the fire's starting size to a decimal, like 5.5 because it will become 4.5 then 3.5, 2.5, 1.5, 0.5, -0.5 and it never became 0 preventing that code from running. That solution is also simple, just replace == with <=.

0
Thanks! Gwolflover 80 — 8y
Ad
Log in to vote
3
Answered by 8 years ago
Edited 8 years ago

The main problem is that you store the size of the fire in the variable Strength so Strength - 1 will be the same value in every loop.

Some other problems I have found is that, Size is in the range of 2~30 and Heat is in the range of 0~25. This information was from my own tests in studio though the wiki page does give different information.

Putting this all together:-

local Campfire = script.Parent.Parent
local Fire = script.Parent.Fire

while true do
    print(Fire.Size)
    wait(5)
    -- we use the current fire size and take one from it
    Fire.Size = Fire.Size - 1

    if Fire.Size == 2 then -- the new min fire limit
        Campfire:Destroy()
    end
end

I hope this helps.

2
You left in :remove() GoldenPhysics 474 — 8y
0
The prints way be from other scripts, the script that Gwolflover provided will only print the same value which was stored in the Strength variable User#5423 17 — 8y
0
@GoldenPhysics ops lol thanks User#5423 17 — 8y
0
Thanks! Gwolflover 80 — 8y
Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Like above, strength is defined outside the loop, and you aren't changing it inside the loop so it will always stay the same. Instead, just change the fire.Size directly by:

Fire.Size = Fire.Size -1

Also use :Destroy() when removing the campfire. remove() is deprecated.

Answer this question