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