Finding the closest part to a player script not working

I am trying to make a system where it finds the closest part to a player. The issue is that I get an error saying “attempt to compare number and nil” even though it prints the number out not nil. I have looked stuff up on the dev fourm and found none of it works for me. I have attached the code below.

for i,plr in ipairs(Players)do
		local char = plr.Character		
		local mag = tonumber((char.HumanoidRootPart.Position - HRP.Position).Magnitude)
		if plr.Name~=Target.Name then
			if Target.Value == nil then
				ClosestMag = mag
				Target.Value = plr
				print(type(ClosestMag))
			elseif ClosestMag>mag then
				ClosestMag = mag
				Target.Value = plr
			end
		end
	end

Thanks for taking your time to read this

2 Likes

Magnitude already returns a number, so you just need to do

local mag = (char.HumanoidRootPart.Position - HRP.Position).Magnitude

Also it could be that ClosestMag is nil? Maybe try this?

for i,plr in ipairs(Players)do
	local char = plr.Character		
	local mag = (char.HumanoidRootPart.Position - HRP.Position).Magnitude
	if plr.Name~=Target.Name then
		if Target.Value == nil or (ClosestMag and ClosestMag > mag) then
			ClosestMag = mag
			Target.Value = plr
			print(type(ClosestMag))
		end
	end
end

I combine the 2 statements since they effectively did the same thing.

It’s likely that your ClosestMag thing is not created properly? How are you making it?

2 Likes

When I combined the two statements it through an error. I already knew making it a magnitude would produce a number but it was saying it was nil so I tried something else. The “ClosestMag” variable was created before the ‘for loop’ and the value is changed to “mag” if “mag” is found to be smaller than the current ClosestMag’s magnitude. I am storing the “Target” value in a ‘ObjectValue’ nested in the script allowing for more flexibility. I have had trouble using values before in place of variables.

Could you printing the thing in mag and the thing in ClosestMag? I still think it’s an issue with ClosestMag, but debugging should show us what’s going on

Alright so I printed the variables after the if statements in the for loop, and this is what I got.

That’s odd…Do you know at which part of the loop it errors? The start? The end? The middle of it?

I run the function in a while loop infinitely, the first stop time the it runs its fine then the second time it always errors.

Do you by any chance change the contents of ClosestMag after that loop is finished? Could I see the full while loop?

Its done in a function so the code I showed you is all the code, the while loop just runs the function thats it.

Could it be that ClosestMag needs to be “reset”? Say you initalize it as

local ClosestMag = 100

For example, maybe you can put ClosestMag = 100 before the ipairs loop?

Thats not really desirable tbh but what I will do is I will put the variables outside of the function except for the char variable.

I managed to fix it by doing the calculation in if statement oppose to doing in it in a variable, here is the code below.

elseif (plr.HumanoidRootPart.Position - HRP.Position).Magnitude < (Target.Value.HumanoidRootPart.Position - HRP.Position).Magnitude then

I not sure why this works, if you do, I would love it if can you respond to this message, thanks.