Attempt to index nil with 'Position'

Hello!

So I’m making a swinging game and it keeps throwing me the error of attempt to index nil with 'Position'. Even though it works perfectly fine before.

This seems to be happening at random times and I don’t know how to fix it. I have it set up so that the local script waits for the HumanoidRootPart and then gets the Position, but that clearly doesn’t work sometimes.

Can anyone help?

Here is my code:

--local script.
elseif isholdingdown == true then
		local A0 = Instance.new('Attachment')
		local A1 = Instance.new('Attachment')
		A0.Parent = workspace.Terrain
		A0.WorldPosition = mousepos
		A1.Parent = player.Character:WaitForChild("fakeHand")
		Rope = Instance.new("RopeConstraint")
		Rope.Attachment0 = A0
		Rope.Attachment1 = A1
		Rope.Parent = player.Character:WaitForChild("fakeHand")
		Rope.Visible = true
		Rope.Thickness = 0.1
		Rope.Color = BrickColor.new("Institutional white")
		Rope.Length = (char:WaitForChild("HumanoidRootPart").Position - target.Position).Magnitude

The problem is the Rope.Length line on the very bottom.

It works at times and then it doesn’t and I don’t really know what else to say lol.

Happy Valentines Day! :heart:
-Earthraphobic2

Try using CFrame/Vector3 instead of Position

1 Like

Now it says invalid argument #2
image

Provide the code you have edited.

local A0 = Instance.new('Attachment')
		local A1 = Instance.new('Attachment')
		A0.Parent = workspace.Terrain
		A0.WorldPosition = mousepos
		A1.Parent = player.Character:WaitForChild("fakeHand")
		Rope = Instance.new("RopeConstraint")
		Rope.Attachment0 = A0
		Rope.Attachment1 = A1
		Rope.Parent = player.Character:WaitForChild("fakeHand")
		Rope.Visible = true
		Rope.Thickness = 0.1
		Rope.Color = BrickColor.new("Institutional white")
		Rope.Length = (char:WaitForChild("HumanoidRootPart").CFrame - target.CFrame).Magnitude

A couple of things:

  • What is char and target within your script?

  • Are you using a RemoteEvent?

Magnitude gets the Length of a Vector3, not a CFrame, so i dont understand why you are doing CFrame - CFrame which is an incorrect usage of CFrame, as its supposed to be Vector3 - Vector3, you can remove the usage of (Difference).Magnitude and replace it with DistanceFromCharacter, which does the exact same thing, here should also be a more “Organized” Approach with your code:

local arm = char:WaitForChild("fakeHand") -- gets arms prior to code
local t = 248 -- short for "tint", Institutional white's RGB code is "248"

local A0, A1 = Instance.new('Attachment'), Instance.new('Attachment') -- A Valid Syntax
A0.Parent = workspace.Terrain; A0.WorldPosition = mousepos -- Also a Valid Syntax
A1.Parent = arm -- Parents "A1"

local rope = Instance.new("RopeConstraint") -- rope
rope.Attachment0 = A0; rope.Attachment1 = A1 -- rope Attachment
rope.Visible = true; -- Visible rope
rope.Thickness = .1 -- Thickness of rope
rope.Color = Color3.fromRGB(t,t,t) -- Applies tint Color Code: (248, 248, 248)
rope.Parent = arm -- Parents Rope

rope.Length = player:DistanceFromCharacter(target.Position) -- Length of Vector3

char is the player character

target is mouse.Target

I tried :DistanceFromCharacter and it still keeps throwing the error randomly. It works and then just doesn’t work, and that’s all that I can tell you, unfortunately, because that’s all I know.

Just add another parentheses on the WaitForChild call. Done.

1 Like

Still sometimes gives me the error.