Bool values wont get set back to false

UIS.InputBegan:Connect(function(input,gpe)
    if gpe then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if mob and isAttacking == false and canAttack == true and canRetreat == false then
                AttackGui.Attack.Text = "RETREAT"
            
            for i,pets in pairs(equippedPets:GetChildren()) do
                pets.Parent = sentPets
        
                RunService.Heartbeat:Connect(function()
                    if mob.Parent ~= nil then
                        isAttacking = true
                        canRetreat = true
                        positionPets(mob.Parent, sentPets)
                    end
                end)
            end
        end
        
        if mob and isAttacking == true and canRetreat == true then
            AttackGui.Attack.Text = "ATTACK"
            
            for i,pets in pairs(sentPets:GetChildren()) do
                
                    pets.Parent = equippedPets
                    
                    RunService.Heartbeat:Connect(function()
                        if mob.Parent ~= nil then
                            isAttacking = false
                            canRetreat = false
                            positionPets(player.Character, equippedPets)
                        end
                    end)
                end
            end
        end
    end)
        

so i think the problem is that im using this in the same uis function but it works just fine until i try to attack after retreating and im printing what isAttacking and canRetreat equal and there not being set back to false

I would suggest separating the functions or disconnecting the functions after they run

There are a couple issues here. One of them is that you’re connecting a heartbeat function every time the client enters some input. You never disconnect the functions, so now you not only have multiple heartbeat events, but they are each running code every frame that may not be necessary.

1 Like

so to disconnect them i can do that like if connection then connection:Disconnect()?

It is probably not necessary to connect the functions at all. Can you tell me exactly what you’re trying to do?
But to answer your question, yes. To disconnect a function you must store it in a variable then disconnect it like so:

myEvent = game:GetService("RunService").Heartbeat:Connect(function()
    -- code here
end)
myEvent:Disconnect()

https://gyazo.com/032b52df5a70c34d5d3918928f2dae34
this is what im trying to do but an inf amount of times (this is what the current script is doing) and where would the smartest place to disconnect it be?