If statement bug, despite meeting all requirements?

This code here is supposed to spread fire, similar to the fire in Natural Disaster Survival. It works when it is fired on by a remote event. It works great the first time you use it. The issue I’m facing starts when you fire the event a second time. For some reason it wont spread the fire. I know everything is working up to this point:

if table.find(found_parts, v) and v:GetAttribute("burned") == nil and v:GetAttribute("wontBurn") == nil and state == 0 then

For some reason the second time the fire spreads it’s not meeting one of these requirements in the if statement . I don’t know which one, though. It should meet all of them. You can see in the image attached it does:

image

Main Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ZoneModule = require(ReplicatedStorage.Zone)

local spreadSpeed = 0.05

--//Select Starting Random Position//
local function choose_start_position()
    print("CHOOSING STARTING POS")
    local container = ZoneModule.new(game.Workspace:FindFirstChild("ZoneContainer"))
    parts = container:getParts()

    local chosenStartingPoint = parts[math.random(1,#parts)]
    return chosenStartingPoint
end

local state = 0

--//Main Loop//
local original_info ={}
local function remote_event(start_part, speed)
    if state == 0 then

        burned_parts = {start_part}

        print(state)
        for _, burned_part in burned_parts do 
            found_parts = workspace:GetPartBoundsInRadius(burned_part.Position,(burned_part.Size.X + burned_part.Size.Y + burned_part.Size.Z) * 0.5) -- Get all parts that the burned part hitbox is touching
            print(found_parts)
            for _, v in found_parts do -- Check if the new part is touching the burned part
                if table.find(found_parts, v) and v:GetAttribute("burned") == nil and v:GetAttribute("wontBurn") == nil and state == 0 then

                    --//Visual Effects//
                    local OriginalColor = v.Color
                    local OriginalMaterial = v.Material
                    table.insert(burned_parts, v)
                    original_info[v] = {OriginalColor, OriginalMaterial}


                    v.Color = Color3.fromRGB(255, math.random(50,144), 0)
                    v.Material = Enum.Material.Neon
                    script.FireParticle:Clone().Parent = v
                    v:SetAttribute("burned", true)

                    task.wait(speed)
                end
            end
        end
    end
end


--//Return Burnt Parts To Their Original Look//
local function return_state(Container, SFX)
    for part, originalData in original_info do
        part.Color = originalData[1]
        part.Material = originalData[2]
        part:FindFirstChild("FireParticle"):Destroy()
    end
    
    for _,v in burned_parts do
        v:SetAttribute("burned", false)
    end
    
    table.clear(burned_parts)
    table.clear(original_info)
    table.clear(found_parts)
    table.clear(parts)
    Container:Destroy()
    SFX.Playing = false
    
    print(burned_parts)
end

--//Connect To Bindable Events//
ReplicatedStorage:WaitForChild("DisastersEvents").Fire.OnServerEvent:Connect(function(Player, Stage)
    if Stage == 0 then
        state = 0
        print("passed")
        Container = script:FindFirstChild("ZoneContainer"):Clone()
        SFX = script.SoundSFX
        
        Container.Parent = game.Workspace
        SFX.Playing = true
        
        remote_event(choose_start_position(), spreadSpeed)
        
    else
        state = 1
        return_state(Container, SFX)
    end
end)

Video:

The best thing for troubleshooting if statements is to print the variable before the if instead of just printing a confirmation that it happened. For example instead of:

if x = 3 then
    print("it worked")
    -- code
end

try

print("x = ", x)
if x = 3 then
    -- code
end

This helps tell you if x is 2.999999 or if x = nil, basically an indication of why it did or didn’t work.

Thanks for the reply! I did do this, you can see it in the image attached.

So print each of these variables before this if to see if burned actually is nil and wontBurn is nil and state is 0. It’ll point you in the direction you need to go when you get a print that is not what you expect.

if table.find(found_parts, v) and v:GetAttribute("burned") == nil and v:GetAttribute("wontBurn") == nil and state == 0 then

Again, I have already attached an image of the exact output you are looking for. Here is that image again for your convenience.
image

Sorry, you didn’t mention that those results were printed inside the script while it was running. All I saw was print("passed"). It looks like they only printed once, but if the print was inside the for _, v loop I was expecting to see the prints from each time it looped through.

How about Stage? You are adding 1 to that in the next section so if you print(state) before the if state == 0 line then that would tell you what’s going on there.

1 Like

This has been solved, it was a really obvious bug on my part, where I was using nil instead of ~= true where the line script was stuck. Ty

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.