This is how to know if the player is dead and if he dies, it is useful for survival games

You can place the function anywhere because it is pure. It is to know if the player dies once or is already dead. That is useful for survival games.

Here is the code:

return function(player: Player, func: () -> ())
	local character = player.Character
	if character ~= nil then
		local humanoid = character:WaitForChild("Humanoid")
		if humanoid:GetState() == Enum.HumanoidStateType.Dead then
			func()
			return
		end
		local humanoidStateChangedConnection
		humanoidStateChangedConnection = humanoid.StateChanged:Connect(function(_, newState)
			if newState == Enum.HumanoidStateType.Dead then
				func()
				humanoidStateChangedConnection:Disconnect()
				return
			end
		end)
		humanoid.Died:Once(function()
			func()
			humanoidStateChangedConnection:Disconnect()
			return
		end)
	end
end
5 Likes

I’m gonna assume you wrotethis because the .Died function doesn’t always trigger? If then I’m not sure if that will always work, I’d also add a .HealthChanged function

1 Like