Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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)
0
What type of script this is exactly? Server-side or Client-side? NotThatFamouss 605 — 2y
0
It's client-sided Ziffixture 6913 — 2y
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. Ziffixture 6913 — 2y
0
See the bottom of the following link for more information: https://developer.roblox.com/en-us/api-reference/property/Player/Character Ziffixture 6913 — 2y
View all comments (3 more)
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. Ziffixture 6913 — 2y
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. Ziffixture 6913 — 2y
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. NotThatFamouss 605 — 2y

1 answer

Log in to vote
2
Answered by 2 years ago

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
Even with the edits, there are still outstanding issues. See my comments for more details. Ziffixture 6913 — 2y
Ad

Answer this question