#Heartbeat / while loop not terminating

1 messages · Page 1 of 1 (latest)

pseudo valve
#

Im having this strange issue where I was using heartbeat and when calling disconnect it still ran a couple times after I disconnected it.

task.spawn(function()
            local connection
            local running = true
            
            connection = RunService.Heartbeat:Connect(function()
                task.wait(0.5)
                print(canSeePlayer)
                if canSeePlayer and running then
                    
                    Path:Stop()
                    Path:Destroy()
                    print("path stopped") --prints like 3-5 times after disconnecting
                    
                    connection:Disconnect()
                    connection = nil
                    running = false
                    
                    --pathStoppedBindable:Fire()
                    chasePlayer(enemy)
                    
                    return
                end
            end)
        end)

That had "path stopped" being printed multiple times even though I Disconnected the connection and set running to false (second check) AND i returned it yet it still ran like 5 times. Someone suggested using a while loop so I tried

task.spawn(function()
            local running = true
            
            while running do
                task.wait(0.5)
                print(canSeePlayer)
                if canSeePlayer and running then

                    Path:Stop()
                    Path:Destroy()
                    print("path stopped") --prints like 3-5 times after running becomes false

                    running = false
                    --pathStoppedBindable:Fire()
                    chasePlayer(enemy)

                    return
                end
            end    
        end)

But im still having the same problem

hexed tide
#

why do you have a wait in there?

#
connection = RunService.Heartbeat:Connect(function()
                task.wait(0.5)```
pseudo valve
#

because i thought that would stop it because it takes a second to stop completely but that didn’t fix it

hexed tide
#

Path:Stop() probably has delay in it, idk you didnt show much code to go by

willow igloo