Attempt to index nil with 'WaitForChild'

I wanted to make the footsteps change with a server script,but for some reason it indexes nil with WaitForChild,how can i fix it? Here’s the script

local speed = 0.7
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	local WalkSound = player.Character:WaitForChild("HumanoidRootPart").Running
	WalkSound.SoundId = 133705377
	WalkSound.PlaybackSpeed = speed
	WalkSound.Volume = 1.5
end)

game:GetService("ReplicatedStorage").Death.OnServerEvent:Connect(function(player)
	local WalkSound = player.Character:WaitForChild("HumanoidRootPart").Running
	WalkSound.SoundId = 133705377
	WalkSound.PlaybackSpeed = speed
	WalkSound.Volume = 1.5
end)

Would really appreciate any help

change the

local WalkSound = player.Character:WaitForChild("HumanoidRootPart").Running

to

local character = game.Workspace:WaitForChild(player.Name)

local WalkSound = character:WaitForChild("HumanoidRootPart"):WaitForChild("Running")

hope this helps thank you

The sound didnt change but theres no errors now,thanks

As XianHao mentioned, you’re getting the character in an unstable way.

I suggest you use local character = player.Character or player.CharacterAdded:Wait()

And then make a HRP variable like local HRP = character:WaitForChild("HumanoidRootPart").

Or use XianHao’s method.

As @lionel760x mentioned and it seemed to work, replace the parts of code that they mentioned, and now, it doesn’t seem like it’s playing the sound, so then add WalkSound:Play()

local speed = 0.7
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	local WalkSound = player.Character:WaitForChild("HumanoidRootPart").Running
	WalkSound.SoundId = 133705377
	WalkSound.PlaybackSpeed = speed
	WalkSound.Volume = 1.5
     WalkSound:Play()
end)

game:GetService("ReplicatedStorage").Death.OnServerEvent:Connect(function(player)
	local character = game.Workspace:WaitForChild(player.Name)

   local WalkSound = character:WaitForChild("HumanoidRootPart"):WaitForChild("Running")
	WalkSound.SoundId = 133705377
	WalkSound.PlaybackSpeed = speed
	WalkSound.Volume = 1.5
    WalkSound:Play()
end)

… This should work, notify me if not.

I mean,the default walksound plays,but not custom

1 Like

Thank you,but is there any way to change the speed of the sounds?

try changing the pitch of the sound

1 Like