# Get neighbours of a position - if statement passing when shouldnt

Hey Devs!

I am currently making a node-based pathfinding system. I am trying to implement a way to get neighbours of a position of a node.

What it would do is wait and if all nodes have been added then add the neighbours. The code below is trying to detect if there is a node between the minimum spacing and maximum spacing of all the 3D ways for a node to be.

The problem is that the if statement still passes even if the supposed neighbour node is further away than the MaxSpacing variable allows. I also believe that there is a better, more optimized way to implement this but I simply cannot think of anything at all.

All help is appreciated! Thanks!

``````local MinSpacing = 10
local MaxSpacing = 50

if (NewNode.Position + Vector3.new(MinSpacing,0,0)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,0,0)).Magnitude
or (NewNode.Position + Vector3.new(0,MinSpacing,0)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(0,MaxSpacing,0)).Magnitude
or (NewNode.Position + Vector3.new(0,0,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(0,0,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,0,0)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,0,0)).Magnitude
or (NewNode.Position + Vector3.new(0,-MinSpacing,0)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(0,-MaxSpacing,0)).Magnitude
or (NewNode.Position + Vector3.new(0,0,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(0,0,-MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(MinSpacing,0,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,0,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(MinSpacing,0,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,0,-MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,0,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,0,-MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,0,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,0,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(MinSpacing,MinSpacing,0)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,MaxSpacing,0)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,MinSpacing,0)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,MaxSpacing,0)).Magnitude
or (NewNode.Position + Vector3.new(0,MinSpacing,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(0,MaxSpacing,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(0,MinSpacing,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(0,MaxSpacing,-MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(MinSpacing,MinSpacing,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,MaxSpacing,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(MinSpacing,MinSpacing,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,MaxSpacing,-MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,MinSpacing,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,MaxSpacing,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,MinSpacing,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,MaxSpacing,-MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(MinSpacing,-MinSpacing,0)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,-MaxSpacing,0)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,-MinSpacing,0)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,-MaxSpacing,0)).Magnitude
or (NewNode.Position + Vector3.new(0,-MinSpacing,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(0,-MaxSpacing,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(0,-MinSpacing,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(0,-MaxSpacing,-MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(MinSpacing,-MinSpacing,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,-MaxSpacing,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(MinSpacing,-MinSpacing,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(MaxSpacing,-MaxSpacing,-MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,-MinSpacing,MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,-MaxSpacing,MaxSpacing)).Magnitude
or (NewNode.Position + Vector3.new(-MinSpacing,-MinSpacing,-MinSpacing)).Magnitude <= v.Position.Magnitude and v.Position.Magnitude <= (NewNode.Position + Vector3.new(-MaxSpacing,-MaxSpacing,-MaxSpacing)).Magnitude then
``````
1 Like

You can use my pathfinding i made. Also i will post an improved version soon. I still dont understand what do you want to achieve. If you know how A* works you get all the neighbors from a current node by giving the positions

Im sorry. But Iām trying to make my own pathfinding. What I am doing is that when a node is created it will try and find its neighbours which can then be used while trying to create a path. It can get its neighbours perfectly well. Its just that for some reason it exceeds the MaxSpacing from 50 studs to 70 studs. this should be stopped by the if statement but it doesnt for some reason