Magnitude Error

Hey, im having an issue. Issue is: “attempt to index nil with position”

here’s the code.

				if v:FindFirstChild("Humanoid") and v.Humanoid.Health > 0 and v ~= character and v:FindFirstChild("Block") == nil then

					local p1 = character.HumanoidRootPart.Position
					if (p1 - v.Parent:FindFirstChild("HumanoidRootPart").Position).Magnitude < 50 then
2 Likes

It looks good, but we cant tell what is character and through which table are u looping. From what I have seen the problem must be in character or in v

1 Like

It sounds like v is the model of an NPC, so I would do this

if (p1 - v:FindFirstChild("HumanoidRootPart").Position).Magnitude < 50 then

still didnt fix it. Same problem.

The error lies somewhere in either lines 3 or 4, could you add some print statements (Indicating v) so we know that the variable you’re trying to create is valid to the script?

You can also try enclosing everything in a pcall

			pcall(function ()
				if v:FindFirstChild("Humanoid") and v.Humanoid.Health > 0 and v ~= character and v:FindFirstChild("Block") == nil then

					local p1 = character.HumanoidRootPart.Position
					if (p1 - v.Parent:FindFirstChild("HumanoidRootPart").Position).Magnitude < 50 then

it is very difficult to help you if you don’t give us more information.

print(v)
					
					local p1 = character.HumanoidRootPart.Position --+ character.HumanoidRootPart.CFrame.lookVector *50
					if (p1 - v:FindFirstChild("HumanoidRootPart").Position).Magnitude < 50 then
						print(v)

the first one did print, 2nd didn’t.

Hm, maybe now print out both p1 and v.HumanoidRootPart.Position? Either 1 of those 2 could potentially be indexed as nil

can you tell what line is the error happening??

I had this error before
try this

if v:FindFirstChild("Humanoid") and v.Humanoid.Health > 0 and v ~= character and v:FindFirstChild("Block") == nil then

				local p1 = character.HumanoidRootPart.Position
				if (Vector3.new(p1) - Vector3.new(v:FindFirstChild("HumanoidRootPart").Position)).Magnitude < 50 then

its an annoying bug, for some reason when you create a variable of a vector, it converts into a (0,0,0) format with not context. @XurySenpai

1 Like

have you tried using waitforchild?

if v:FindFirstChild("Humanoid") and v.Humanoid.Health > 0 and v ~= character and v:FindFirstChild("Block") == nil then

local p1 = character.HumanoidRootPart.Position
if (p1 - v.Parent:WaitForChild("HumanoidRootPart").Position).Magnitude < 50 then

Hey, I tried doing this. This almost worked but not fully, it still can’t find the humanoidrootpart for some weird reason. This is my current code:

local p1 = character:WaitForChild("HumanoidRootPart").Position
if (Vector3.new(p1) - Vector3.new(v:WaitForChild("HumanoidRootPart").Position)).Magnitude < 50 then

Maybe because when your actually running the game, there is no player? I see that you are trying to get the magnitude of two players and displaying it. You may want to use the pcall function so the script doesn’t need to run into errors.

So you might want to run your code in a pcall function.

Sorry for the late reply i was busy…

It is good practice to check if something exists before attempting to use its properties.

You really should not do: v.Parent:FindFirstChild(“HumanoidRootPart”).Position
in the case that HumanoidRootPart does not exist, as this will error.

2 Likes

Hello everyone, I’ve fixed the bug. i kinda let it be for a while but it’s fixed. It was quite a stupid bug and I’m ashamed for not having realized it. There were some NPCs without an humrp. Yeah obviously it won’t find it if there’s none. Anyways that’s it.

2 Likes