Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to index nil with 'WaitForChild', with HumanoidRootPart.??

What i am trying to achieve:

In this script I am trying to spawn a part on the Cframe of the characters HumanoidRootPart

The problem: Keep getting an error message I dont know how to fix. "Players.snipperdiaper.Backpack.Tool.LocalScript:4: attempt to index nil with 'WaitForChild'"

Anyone know why this keeps happening?

Line in question:

humanoidRootPart = character:WaitForChild("HumanoidRootPart")

Complete Script (Not finished with the CFrame spawning yet)

local tool = script.Parent
player = game.Players.LocalPlayer
character = game.Players.LocalPlayer.Character
humanoid = character:WaitForChild("Humanoid")
humanoidRootPart = character:WaitForChild("HumanoidRootPart")


tool.Activated:Connect(function()
	
	local part = game.Workspace:FindFirstChild("BariWall")
	local ClonedPart = part:Clone()
	part.Parent = workspace
	
end)

7 comments

0

What type of script this is exactly? Server-side or Client-side?

0

It's client-sided

0

First off, the character is not guaranteed to have loaded. You'll need to set up logic to wait until it does if need be. Assuming this LocalScript is a descendant of StarterPack, you'll need to take extra steps to ensure you're accessing the latest character.

0

See the bottom of the following link for more information: https://developer.roblox.com/en-us/api-reference/property/Player/Character

0

Secondly, since you're using a LocalScript, the creation of your BariWall clone will not be replicated. This is fine for VFX, but you'll still need to contact the server so it may instruct other clients to raise the same wall.

0

P.S. You're using FindFirstChild inappropriately. You should barely find yourself using its return value without checking it beforehand, if not at all. Its overall prescense isn't warranted, too. I also don't see a good reason for BariWall to rest in Workspace. ReplicatedStorage seems more appropriate.

0

Please post on the answer section instead of the comment section if you are posting an answer/explanation and not a simple question or stuff.

1 answer

✓ Accepted answer

always put local before each variable and change the character variable to this:

local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")


tool.Activated:Connect(function()

    local part = game.Workspace:FindFirstChild("BariWall")
    local ClonedPart = part:Clone()
    part.Parent = workspace

end)

I hope it helps ^^

1 comment

1

Even with the edits, there are still outstanding issues. See my comments for more details.

This discussion is preserved as it was. New answers, comments, and votes are closed.