Why does this not work?

My script to show how long someone survived is not working. I will label where the error is.

local pLayers = game:GetService("Players")

pLayers.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Time = Instance.new("IntValue")
	Time.Name = "Time Alive"
	Time.Value = 0
	Time.Parent = leaderstats
	
	task.wait()
	
	local humanoid = player.Character.WaitForChild("Humanoid") -- error here
	
	while task.wait(1) do
		if humanoid.Health == 0 then
			Time.Value = 0
			task.wait(3)
		else
			Time.Value += 1
		end
	end
end)

Change:

To:

local humanoid = player.Character:WaitForChild("Humanoid") 

You put “.” there it should be “:

local humanoid = player.Character:WaitForChild("Humanoid")

@Frizionek
@CZXPEK

I changed this and it still errors in the same line.

its probably because the players character hasnt loaded yet

local pLayers = game:GetService("Players")

pLayers.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Time = Instance.new("IntValue")
	Time.Name = "Time Alive"
	Time.Value = 0
	Time.Parent = leaderstats

	player.CharacterAdded:Wait()

	local humanoid = player.Character:WaitForChild("Humanoid") -- error here

	while task.wait(1) do
		if humanoid.Health == 0 then
			Time.Value = 0
			task.wait(3)
		else
			Time.Value += 1
		end
	end

end)

So how would I fix that?

I did local char = player:WaitForChild(“Character”) and it still won’t work. the output says “Infinite yield possible” on it

i attached the fixed version to my post above

Try this:

local pLayers = game:GetService("Players")

pLayers.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Time = Instance.new("IntValue")
	Time.Name = "Time Alive"
	Time.Value = 0
	Time.Parent = leaderstats
	
	task.wait()
	
    local character = player.Character or player.CharacterAdded:Wait() --Sets character to character, if nil then it waits for it
	local humanoid = character:FindFirstChild("Humanoid")
	
	while task.wait(1) do
		if humanoid.Health == 0 then
			Time.Value = 0
			task.wait(3)
		else
			Time.Value += 1
		end
	end
end)