Why does it say the humanoid has died when it first spawns in?

Basically trying to make a custom spawn type script and its not going to well

function AddGui(Player)
	local gui = game.StarterGui.Main:Clone()
	gui.Parent = Player.PlayerGui
end

function PlayerDied(Player)
	wait(2)
	Player.Character:Remove()
end

function AddedPlayer(Player)
	AddGui(Player)
	
	if not Player.Character then repeat wait() until Player.Character end
	local Character = Player.Character

	while wait() do
		if Character.Humanoid.Died  then
			print("Died")
			PlayerDied(Player)
			break
		end
	end
end

Players.PlayerAdded:Connect(AddedPlayer)

But whenever the player first spawns it it counts as the player has died for some reason, it never happens on any spawn in after the first

Just wondering why this is happening and what i can do to fix it or something

1 Like

Try

while wait() do
		Character.Humanoid.Died:Connect(function()
			print("Died")
			PlayerDied(Player)
			break
		end)
	end

Remove the while true do and do what @BaconKral suggested with the Humanoid.Died connection (not sure why he left the while true do).

function AddGui(Player)
	local gui = game.StarterGui.Main:Clone()
	gui.Parent = Player.PlayerGui
end

function PlayerDied(Player)
	wait(2)
	Player.Character:Remove()
end

function AddedPlayer(Player)
	AddGui(Player)
	
	if not Player.Character then repeat wait() until Player.Character end
	local Character = Player.Character

	Character.Humanoid.Died:Connect(function()
		print("Died")
		PlayerDied(Player)
	end)
end

Players.PlayerAdded:Connect(AddedPlayer)

It works but now it only works once, after the first death it no longer recognizes the character

When the player dies their character gets ‘replaced’, so it’s a new Humanoid instance. You’ll need to re-connect to the humanoid when the player respawns.

Try this instead:

function AddGui(Player)
	local gui = game.StarterGui.Main:Clone()
	gui.Parent = Player.PlayerGui
end

function PlayerDied(Player)
	wait(2)
	Player.Character:Remove()
end

function AddedPlayer(Player)
	AddGui(Player)
	
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function()
			print("Died")
			PlayerDied(Player)
		end
	end
end

Players.PlayerAdded:Connect(AddedPlayer)
1 Like