How to tell when player has fully respawned?

local function onPlayerAdded(player)
	player.CharacterAdded:connect(function(character)
		character.Humanoid.Died:Connect(function()
			repeat wait() until character
			if not configuration.GAME_RUNNING then return end
			print('top alive')
		end)
	end)
	
	if player.Character and player.Character.Parent then
		player.Character.Humanoid.Died:Connect(function()
			repeat wait() until player.Character
			if not configuration.GAME_RUNNING then return end
			print('bottom alive')
		end)
	end
end

I’m trying to get the prints to fire when a player has died and fully respawned. For some reason, repeat wait() until player.Character doesn’t wait until the player has respanwed. Also, for those who might ask, why I have 2 sections doing the same thing, I’ve found that

player.CharacterAdded:connect(function(character)
    		character.Humanoid.Died:Connect(function()

does not function when a player dies for the first time. I have no clue why, but yeah. So that’s why I have the second function, which literally just picks up when the player dies for the first time. Because when I check the output for a player dying it prints ‘bottom alive’ then the second time and every other time it’s ‘top alive’ so if any one knows why that is that’d would fantastic :smiley:

But main question is why does it fire the prints when the player has died and not after they have respawned??

14 Likes

You are using the variable character and not player.Character. Also use events over loops.

game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(charModel)
		charModel:WaitForChild("Humanoid").Died:Connect(function()
			print(plr.Name, "Died")
			
			plr.CharacterAdded:Wait()
			
			print(plr.Name, "Spawned")
		end)
	end)
end)
18 Likes

Just a small note on this line:

repeat wait() until character

This will always do exactly one wait() so it is not necessary to have this statement. The only way that “character” (the parameter fed into this function) is undefined is if you set it to nil yourself. Variables can’t spontaneously become nil, and instances are not garbage collected as long as you have a reference to that instance.

9 Likes

Sorry if I’m being dumb but surely he could just put the code in the CharacterAdded block. Because CharacterAdded will only run when they first join and then when the character is added again which only happens when they die.

1 Like

I also thought that when making the code but idk the use case so I did not change the code.

1 Like

You can just put the code that you need in the CharacterAdded block as that will only run when the character is first added when you join and then when you die.

It’s redundant to have a wait till the character exists after the user dies.