Why is my script not detecting the humanoid root part?

Hello everyone,
I made this script, and I’ts not detecting the player’s primary part. I tried to use repeat, but it says attempt to index nil with FindFirstChild

game.Players.PlayerAdded:Connect(function(player)
	if player.Character:FindFirstChild("HumanoidRootPart") then
		local folder = Instance.new("Folder", player)
		folder.Name = "Music"

		local sound = Instance.new("StringValue", folder)
		sound.Name = "SoundID"
		sound.Value = "0"

		local dp = Instance.new("StringValue", folder)
		dp.Name = "DeathID"
		dp.Value = "0"

		local audio = Instance.new("Sound", player.Character:FindFirstChild("HumanoidRootPart"))
		audio.SoundId = sound.Value
		audio.Name = "MusicSound"

		audio.Volume = 1
		audio.Looped = true

		player.Character.PrimaryPart:FindFirstChild("Died").SoundId = dp.Value

		sound:GetPropertyChangedSignal("Value"):Connect(function()
			audio.SoundId = sound.Value
		end)

		dp:GetPropertyChangedSignal("Value"):Connect(function()
			player.Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Died").SoundId = dp.Value
		end)
	end
end)

Error:
image
Any help is appreciated

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	if player.Character:FindFirstChild("HumanoidRootPart") then
		local folder = Instance.new("Folder", player)
		folder.Name = "Music"

		local sound = Instance.new("StringValue", folder)
		sound.Name = "SoundID"
		sound.Value = "0"

		local dp = Instance.new("StringValue", folder)
		dp.Name = "DeathID"
		dp.Value = "0"

		local audio = Instance.new("Sound", player.Character:FindFirstChild("HumanoidRootPart"))
		audio.SoundId = sound.Value
		audio.Name = "MusicSound"

		audio.Volume = 1
		audio.Looped = true

		player.Character.PrimaryPart:FindFirstChild("Died").SoundId = dp.Value

		sound:GetPropertyChangedSignal("Value"):Connect(function()
			audio.SoundId = sound.Value
		end)

		dp:GetPropertyChangedSignal("Value"):Connect(function()
			player.Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Died").SoundId = dp.Value
		end)
	end
end)
1 Like

It works but, that gave me another error:

On this line:

player.Character.PrimaryPart:FindFirstChild("Died").SoundId = dp.Value

image

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	if player.Character:FindFirstChild("HumanoidRootPart") then
		local folder = Instance.new("Folder", player)
		folder.Name = "Music"

		local sound = Instance.new("StringValue", folder)
		sound.Name = "SoundID"
		sound.Value = "0"

		local dp = Instance.new("StringValue", folder)
		dp.Name = "DeathID"
		dp.Value = "0"

		local audio = Instance.new("Sound", player.Character:FindFirstChild("HumanoidRootPart"))
		audio.SoundId = sound.Value
		audio.Name = "MusicSound"

		audio.Volume = 1
		audio.Looped = true

		player.Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Died").SoundId = dp.Value

		sound:GetPropertyChangedSignal("Value"):Connect(function()
			audio.SoundId = sound.Value
		end)

		dp:GetPropertyChangedSignal("Value"):Connect(function()
			player.Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Died").SoundId = dp.Value
		end)
	end
end)
1 Like

try :WaitForChild() instead
chars

1 Like