Wait Data value

this script is in StarterPlayerScripts.

game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid").Died:Connect()

when game starts, the LocalPlayer.Character is nil.
but, i can’t use LocalPlayer:WaitForChild("Character") because of it’s not a instance.

so, it making error " attempt to index nil with ‘WaitForChild’ "

how to wait Data value?

Player has a event called CharacterAdded.
Use this:

local Player = game:GetService("Players").LocalPlayer
game:GetService("Players").LocalPlayer.CharacterAdded:Connect(function()
Player.Character:WaitForChild("Humanoid").Died:Connect(function()
print("Humanoid died!")
end)
end)

Also, Humanoids, and pretty much characters, get destroyed whenever they die, so if you need to constantly check if a player dies, the function above will guarantee that. Most use CharacterAdded:Wait() which only runs once, but functions run until disconnected.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
	print("Died.")
end)

Characters get reset when the humanoid dies, so, once the character dies, you begin referencing nil.

https://developer.roblox.com/en-us/api-reference/class/StarterCharacterScripts

My problem with your proposed solution is that you keep on indexing instances you already have references to.

Local scripts inside of StarterCharacterScripts are cloned into the player’s character each time it spawns/reloads (is added).

If you were hellbent on having this script inside of StarterPlayerScripts then you could do the following.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

Player.CharacterAdded:Connect(function(Character)
	local Humanoid = Character:WaitForChild("Humanoid")
	Humanoid.Died:Connect(function()
		print("Died.")
	end)
end)
1 Like

I don’t know if one of us is misunderstanding the other, but what I mean is that a :Wait function yields and only works once. Once a player dies, their character is destroyed and replaced with the StarterPlayer model (Or the default). This is to remove unneeded parts, scripts, etc, so, since CharacterAdded only works once, it doesn’t know if it happens again unless it is connected to a function and repeatedly resets the Humanoid.Died function (The previous one will be disconnected automatically because that’s what ROBLOX does)

My bad. I see your point now, it appears to be on my side here. Thanks for clarification.