Checking if a part is close to another part but only on the x and z axis?

Im trying to check and see if a humanoids root part is close to a vector3 position but I only want to check the x and z axis. I dont care were it is on the y axis.
this is what I got so far but it seems this does not work.

repeat
			wait()
		until
		script.Parent.Parent.HumanoidRootPart.Position.X <= closestorb.Center.Position.X + 20 or
		script.Parent.Parent.HumanoidRootPart.Position.X <= closestorb.Center.Position.X + -20 or
		script.Parent.Parent.HumanoidRootPart.Position.Z <= closestorb.Center.Position.Z + 20 or
		script.Parent.Parent.HumanoidRootPart.Position.Z <= closestorb.Center.Position.Z + -20
1 Like

The best way to do this is to create new vectors3 without the Y axis then subtract them and get the magnitude.

local NewHumanoidRootPosition = (script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(Vector3.new(1,0,1)))
local NewCosestOrbPosition = (closestorb.Center.CFrame * CFrame.new(Vector3.new(1,0,1)))

local Distance = (NewHumanoidRootPosition-NewCosestOrbPosition).magnitude

1 Like

so I put that in the repeat loop like so

repeat
			wait()
			local newhumpos = (script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(Vector3.new(1,0,1)))
			local neworbpos = (closestorb.Center.CFrame * CFrame.new(Vector3.new(1,0,1)))
			local total = (newhumpos - neworbpos).Magnitude
		until

but since Im waiting for the humanoid root part to be close to “closestorb”, what should I put below “until”?

1 Like
			wait()
			local newhumpos = (script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(Vector3.new(1,0,1)))
			local neworbpos = (closestorb.Center.CFrame * CFrame.new(Vector3.new(1,0,1)))
			local total = (newhumpos - neworbpos).Magnitude
		until total <= 20

there seems to have been an error with the value “total”

vector3 expected got cframe

lines used:

repeat
			wait()
			local newhumpos = (script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(Vector3.new(1,0,1)))
			local neworbpos = (closestorb.Center.CFrame * CFrame.new(Vector3.new(1,0,1)))
			local total = (newhumpos - neworbpos).Magnitude
		until
		total <= 20
1 Like

made some minor adjustments and this works:

repeat
			wait()
			local newhumpos = (script.Parent.Parent.HumanoidRootPart.CFrame * Vector3.new(1,0,1))
			local neworbpos = (closestorb.Center.CFrame * Vector3.new(1,0,1))
			local total = (newhumpos - neworbpos).Magnitude
		until
		total <= 20
1 Like