Ending a Function When Player Dies

I’m trying to end this function when the humanoid died event fires but I can’t figure out what I’m doing or how to do it. As far as I know, simply returning end in the event won’t work. Is there another way I can end the function and break the loop when the event fires?

	["SinglePlayer"] = function(tileTable, player)
		local humanoid = player.Character:FindFirstChild("Humanoid")
		humanoid.Died:Connect(function()
			--idk what to put here lol
			return --something idk
		
		end)
		detectClicks(tileTable)
		local repeatTimes = #tileTable
		for i = 1, repeatTimes do
			local c = tileTable[math.random(1, #tileTable)]
			local fadeOut = tservice:Create(c, tinfoOut, {Transparency = 1})
			if c then
				fadeOut:Play()
				wait(tinfoOut.Time)
				c:Destroy()
			end
			wait(5)
		end
	end
1 Like

Try to check if the player is dead inside the loop.

1 Like

I agree with @L_umin. I would think that you need to break where it is being looped (if they are dead), but is the function called once and the loop keeps the function running or is the function being called multiple times?

The function is called multiple times but the loop finishes before the function is called again.

I would check on both then. Because that way the function (and loop) only continue IF you are alive and are ignored if you are dead.

Check if the player is dead before doing anything else in the loop, then it won’t repeat if the player died.

I got it working but I’m just wondering now whether you recommend checking the humanoid’s health, which I am doing currently, or if I should set a boolean value and switch it to false when the player dies.

Both should work fine.
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

2 Likes