Need help debugging my listening connection for an attribute change in Roblox

26 Views Asked by At

I am trying to write a function for a cannon that will activate a firing function when the cannon's attribute "fire" is set to TRUE. When I switch the cannon's attribute "Fire" from true to false the GetAttributeChangedSignal function won't activate at all. Any insight would be greatly appreciated. Also the self.CannonModel is a boat model with several cannons attached.I have yet to get the print statement "attribute changed" to work.

function Cannon:WatchForFire()

    for i,v in pairs(self.CannonModel:GetDescendants()) do 

        if v.Name ~= "Cannon" then
            continue
        end

        --filter to prevent multiple connections for one cannon
        if v:GetAttribute("Connected") == true then
            continue
        end

        --Create listening connection to activate when attribute to changes
        local fireConnection = v:GetAttributeChangedSignal("Fire"):Connect(function()
            print("attribute changed")
            if v:GetAttribute("Fire") == true then
                --self:Fire()
                print("IT WORKED")
            end
        end)

        table.insert(self.AllConnections, fireConnection)

        v:SetAttribute("Connected",true)

    end
end

I have double checked that the cannon's "Fire" attribute is switching from true to false and that the v in the loop above is a cannon. I am very new to Roblox so if anyone has any insight I would be very grateful.

0

There are 0 best solutions below