BodyPosition Position

I wanted to try out BodyPosition.
However, I ran into a strange issue while trying to do so:

  12:09:41.033 - Workspace.GhostArm.Script:5: attempt to index nil with 'WaitForChild'

And here’s the code:

local plrs = game:GetService("Players")
local runs = game:GetService("RunService")
local bod = script.Parent:WaitForChild("BodyPosition")
local char = plrs:WaitForChild("nmh4").Character
local hrp = char:WaitForChild("HumanoidRootPart")


local function follow()
	bod.Position = hrp.Position
end


runs.Heartbeat:Connect(follow)

I had the same error without the variables because I tried to add the variables in case it would fix it.

Am I missing something?

2 Likes

The error your getting is self explanatory but I will tell you what it means anyway

Any of your three variables which use

:WaitForChild

in them can be the problem why you might ask?

Because your trying to find something that is nil just like what is said in the error which means what your trying to find doesn’t exist and your trying to get it using

:WaitForChild

Which is useless in this case how to fix this?

Basically try to find out what is the nil object/value here and fix it by actually putting an object/value

Your code doesn’t tell us much because I can’t see the entire script or what’s in the “GhostArm” that is said in the error.

EDIT: Using wait() somewhere could also fix the problem since maybe something is trying to load but it can’t load fast enough.

The error is that the player character doesn’t exist yet.
Start the script off by using the player.CharacterAdded event, I will show an example

local runs = game:GetService("RunService")
local bod = script.Parent:WaitForChild("BodyPosition")
local plr = plrs:WaitForChild("nmh4")

Here you put in the characteradded function
Made a global variable for the character and the humanoidrootpart whenever the character spawns.

local function onCharacterAdded(character)
    char = character
    hrp = char:WaitForChild("HumanoidRootPart")
end

Here you check whether the humanoidrootpart and the character exists

local function follow()
        if char and hrp then
	    bod.Position = hrp.Position
        end
end

Here is the event

plr.CharacterAdded:connect(onCharacterAdded)
runs.Heartbeat:Connect(follow)

I hope this helps, I am sorry if this is unclear, this is my first real post on this forum. I am new here, but still quite experienced in scripting, please ask me if you want something to be more clear!

2 Likes