Numerating points to follow by script

While I was making the nodes that the part needed to follow I’ve ran into this problem: numerating each node is pretty hard, also because if you make an error while numerating, you need to change every single node above it

So, I need a script that will find the next node and numerate it

I’ve tried making something but it didn’t work, since I’m not really good at scripting. if someone is interested, here there is the code:

for i = 1, #Nodes:GetChildren() do
		
		local closestPart, closestPartMagnitude

		local tmpMagnitude
		
		for i, v in pairs(Nodes:GetChildren()) do
			if closestPart then
				tmpMagnitude = (Node.Position - v.Position).magnitude


				if tmpMagnitude < closestPartMagnitude then
					if closestPart.Orientation.Y > Node.Orientation.Y -50 and closestPart.Orientation.Y < Node.Orientation.Y +50 then
						closestPart = v
						closestPartMagnitude = tmpMagnitude
					else
						print("next node not found!")
					end
				end
			else
				closestPart = v
				closestPartMagnitude = (Node.Position - v.Position).magnitude
			end
		end
		
		Val = Val+1
		closestPart.Name = "Node"..Val
		Node = Nodes["Node"..Val]
		
		
		return closestPart, closestPartMagnitude
	end

if someone know how to fix this I would really appreciate it

3 Likes

well, I’ve fixed this:

function FindNearest(part,Group,Number)
	
	local closestPart, closestPartMagnitude

	local tmpMagnitude
	
	for i, v in pairs(Group:GetChildren()) do
		
		if part.Orientation.Y < 0 then
			local PartRot = math.abs(part.Orientation.Y) 
			local VRot = math.abs(v.Orientation.Y)

			if v.Name == "Node" and VRot >= PartRot then

				if closestPart then
					tmpMagnitude = (part.Position - v.Position).magnitude


					if tmpMagnitude < closestPartMagnitude then
						closestPart = v
						closestPartMagnitude = tmpMagnitude 					
					end
				else
					closestPart = v
					closestPartMagnitude = (part.Position - v.Position).magnitude
				end
			end
		elseif part.Orientation.Y > 0 then
			local PartRot = math.abs(part.Orientation.Y) 
			local VRot = math.abs(v.Orientation.Y)

			if v.Name == "Node" and VRot <= PartRot and PartRot-VRot < 40 then

				if closestPart then
					tmpMagnitude = (part.Position - v.Position).magnitude


					if tmpMagnitude < closestPartMagnitude then
						closestPart = v
						closestPartMagnitude = tmpMagnitude 					
					end
				else
					closestPart = v
					closestPartMagnitude = (part.Position - v.Position).magnitude
				end
			end
		end
		
	end
	
	closestPart.Name = "Node"..Number

	return closestPart, closestPartMagnitude
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.