Why runService.heartbeat continues to run?

I know title itself doesn’t explain much but,

I am trying to write this code for adding tag after player died and removing it after 10 second pass
for the first time it works fine it counts to 10 then removes but the issue is heartbeat continues to count to 10 even player doesn’t die again
so the question is how can I make it count to 10 after only death?, or how can I stop it counting after removing tag

local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			--Ölen adamın yeri 
			local Creator = Humanoid:FindFirstChild("creator")
			player.leaderstats.KillStreak.Value = 0 -- ölen adam
			if not CollectionService:HasTag(player,"DeadSafeTag") then
				CollectionService:AddTag(player,"DeadSafeTag")
			end
			if CollectionService:HasTag(player,"DeadSafeTag") then
				remotes.STuffRemover.OnServerEvent:Connect(function()
					CollectionService:RemoveTag(player,"DeadSafeTag")
				end)
			       RunService.Heartbeat:Connect(function(delta)
					DeathTime += delta
					if DeathTime >= 10 then
						CollectionService:RemoveTag(player,"DeadSafeTag")
						DeathTime = 0	
					end
					
			     end)
			end
                 end)

Try to create boolean like this:

local IsEnded = false
local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
                         IsEnded = false
			--Ölen adamın yeri 
			local Creator = Humanoid:FindFirstChild("creator")
			player.leaderstats.KillStreak.Value = 0 -- ölen adam
			if not CollectionService:HasTag(player,"DeadSafeTag") then
				CollectionService:AddTag(player,"DeadSafeTag")
			end
			if CollectionService:HasTag(player,"DeadSafeTag") then
				remotes.STuffRemover.OnServerEvent:Connect(function()
					CollectionService:RemoveTag(player,"DeadSafeTag")
				end)
			       RunService.Heartbeat:Connect(function(delta)
                                        if IsEnded == false then
					DeathTime += delta
					if DeathTime >= 10 then
						CollectionService:RemoveTag(player,"DeadSafeTag")
						DeathTime = 0
                                                IsEnded = true
                                                 end	
					end
					
			     end)
			end
                 end)

no sadly it still continues to count

Btw yes I still didn’t figure our why it is happening or how to fix.

You need to destroy the RunService Heartbeat connection

local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
    local Creator = Humanoid:FindFirstChild("creator")
    player.leaderstats.KillStreak.Value = 0
    if not CollectionService:HasTag(player,"DeadSafeTag") then
        CollectionService:AddTag(player,"DeadSafeTag")
    end
    if CollectionService:HasTag(player,"DeadSafeTag") then
        remotes.STuffRemover.OnServerEvent:Connect(function()
            CollectionService:RemoveTag(player,"DeadSafeTag")
        end)

        local connection: RBXScriptConnection --sets connections
        connection = RunService.Heartbeat:Connect(function(delta)
            DeathTime += delta
            if DeathTime >= 10 then
                CollectionService:RemoveTag(player,"DeadSafeTag")
                DeathTime = 0

                connection:Disconnect() --disconnects after 10 seconds
                connection = nil
            end
        
        end)
    end
end)
2 Likes

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