Best way to stop a connection on player death until respawn without a script in startercharacter?

Trying to have a script stop a connection for a bit when a player dies and come back after the player respawns and tried this

local function LoopCheck()
	local Character = Player.Character or Player.CharacterAdded:Wait()
	
	Connection1 = Character.ChildAdded:Connect(EquippedItem)
	
	local Coro = coroutine.create(function()
		while task.wait() do
			if not Character or Character.Humanoid.Health == 0 then
				Connection1:Disconnect()
				Character = Player.Character or Player.CharacterAdded:Wait()
				Connection1 = Character.ChildAdded:Connect(EquippedItem)
			end
		end
	end)
end

But this doesnt work and i dont really got any other ideas currently on how i could do this

The Character variable won’t just magically disappear, you have to do if not Player.Character or Player.Character.Humanoid.Health == 0 and then you need to wait for the character to despawn before just assigning the character variable again.

alright, but how would i wait for a player’s respawn to restart the connection?
(couldn’t think of a method for character despawn)

you wait for player.CharacterAdded. Just remove the or statement and only keep Player.CharacterAdded:Wait()

1 Like

Is this a server script? This will cause a memory leak if so.

local Coro = coroutine.create(function()
	while task.wait() do
		if not Character or Character.Humanoid.Health == 0 then
			Connection1:Disconnect()
			Character = Player.Character or Player.CharacterAdded:Wait()
			Connection1 = Character.ChildAdded:Connect(EquippedItem)
		end
	end
end)

Not exactly. It disconnects the connection1 and then reconnects it after waiting for the character to be added. Therefore, no memory leaks should be present.

Memory leaks aren’t limited to event connections, that while loop is never broken out of (which could cause alternate threads to persist in memory forever).

Well, the wait statement can never be disconnected. Tell me if I’m wrong, but you can only use it once so putting it in a loop won’t cause leaks.

while task.wait() do
	if not Character or Character.Humanoid.Health == 0 then
		Connection1:Disconnect()
		Character = Player.Character or Player.CharacterAdded:Wait()
		Connection1 = Character.ChildAdded:Connect(EquippedItem)
	else
		break --Break loop in order for the coroutine to be closed (as it reaches the end of its execution).
	end
end

If this is a local script inside the StarterCharacterScripts container then this isn’t necessary (as the script is destroyed when the character is removed anyway) otherwise this is necessary.

1 Like

Oh yeah, I forgot that this was located in a coroutine.

wouldn’t it break if there is a character and not work for any future character spawns?

Lacking information regarding the script’s contents and location, potentially, indefinite loops should still have a breaking condition though, if not player.Parent then may be a more fitting breaking condition.

1 Like

its in starterplayer, wouldn’t just disappear after the player disconnects? plus i didnt see a need for a break if i needed it to run for the entire time

It would depend on how frequently ‘LoopCheck’ is called per client, if it’s only once then yes, the ‘StarterPlayerScripts’ container would be fine.

2 Likes

Yea, it only gets called once as the coroutine handles if the player dies / respawns