How can I make a parts connect to other parts?

I want to make it so parts like this:

Connect like this:

I want to do this for a long time with a pathfinding system but I dont know how

I have asked around a lot looked everywhere but I can find out how to do it.

This is as far as I have gotten:

local PathfindingService = game:GetService("PathfindingService")

local endpoint = workspace.endpoint
local startpoint = workspace.startpoint

local path = PathfindingService:CreatePath()

path:ComputeAsync(startpoint.Position, endpoint.Position)

local waypoints = path:GetWaypoints()

local Folder = Instance.new("Folder")
Folder.Name = "Waypoints"
Folder.Parent = workspace

for i, v in pairs(waypoints) do
	local Part = Instance.new("Part")
	Part.Position = v.Position
	Part.Anchored = true
	Part.Size = Vector3.new(1, 1, 1)
	Part.Parent = Folder
	
	local Part2 = Instance.new("Part")
	Part2.Position = (v.Position - Part.Position).Magnitude
	Part2.Anchored = true
	Part2.Size = Vector3.new(1, 1, 1)
	Part2.Parent = Folder
end

But I get this error when I do that and I dont know what to do and im really stuck please help

image

PLEASE SOMEONE HELP ME IM REALLY STUCK PLEASE

You can raycast and make the part along the raycast.

Which line is line 25? Mine says it is

Part2.Parent = Folder

Which can’t be right. Can you click on that error and see which line it takes you to?

If you read that was just because I forgot to put Vector3.new() and NO that does not dix anything so actually helpo

Dont know how and I DOUBT that will help

local PathfindingService = game:GetService("PathfindingService")

local endpoint = workspace.endpoint
local startpoint = workspace.startpoint

local path = PathfindingService:CreatePath()

path:ComputeAsync(startpoint.Position, endpoint.Position)

local waypoints = path:GetWaypoints()

local Folder = Instance.new("Folder")
Folder.Name = "Waypoints"
Folder.Parent = workspace

for i, v in ipairs(waypoints) do
	local Part = Instance.new("Part")
	Part.Position = v.Position
	Part.Anchored = true
	Part.Size = Vector3.new(1, 1, 1)
	Part.Parent = Folder
	if (i > 1) then
		local lastWaypoint = waypoints[i - 1]
		local dist = (v.Position - lastWaypoint.Position).Magnitude
		local Part2 = Instance.new("Part")
		Part2.Anchored = true
		Part2.BrickColor = BrickColor.Black()
		Part2.Size = Vector3.new(.5, .5, dist)
		Part2.CFrame = CFrame.new(lastWaypoint.Position, v.Position) * CFrame.new(0, 0, dist / -2)
		Part2.Parent = Folder
	end
end

Tested:

3 Likes

Yes, But how would I tween the start point to all of the waypoints?

Because it does not reach the lasy waypoint

local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")

local endpoint = workspace.endpoint
local startpoint = workspace.startpoint

local path = PathfindingService:CreatePath()

path:ComputeAsync(startpoint.Position, endpoint.Position)

local waypoints = path:GetWaypoints()

local Folder = Instance.new("Folder")
Folder.Name = "Waypoints"
Folder.Parent = workspace

for i, v in ipairs(waypoints) do
	local Part = Instance.new("Part")
	Part.Position = v.Position
	Part.Anchored = true
	Part.Size = Vector3.new(1, 1, 1)
	Part.Parent = Folder
	if (i > 1) then
		local tweenInfo = TweenInfo.new(
			1, -- Time
			Enum.EasingStyle.Linear, -- EasingStyle
			Enum.EasingDirection.Out, -- EasingDirection
			0, -- RepeatCount (when less than zero the tween will loop indefinitely)
			false, -- Reverses (tween will reverse once reaching it's goal)
			0 -- DelayTime
		)

	

		local lastWaypoint = waypoints[i - 1]
		local tween = TweenService:Create(startpoint, tweenInfo, {Position = lastWaypoint.Position})
		tween:Play()
		local dist = (v.Position - lastWaypoint.Position).Magnitude
		local Part2 = Instance.new("Part")
		Part2.Anchored = true
		Part2.BrickColor = BrickColor.Black()
		Part2.Size = Vector3.new(.5, .5, dist)
		Part2.CFrame = CFrame.new(lastWaypoint.Position, v.Position) * CFrame.new(0, 0, dist / -2)
		Part2.Parent = Folder
	end
end

the lastWaypoint variable represents the previous waypoint in the iteration, not the current one. Set the Position to v.Position when you create the Tween.

Yes but this is not working

local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")

local TweenInfoNew = TweenInfo.new(
	5, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local endpoint = workspace.endpoint
local startpoint = workspace.startpoint

local path = PathfindingService:CreatePath()

path:ComputeAsync(startpoint.Position, endpoint.Position)

local waypoints = path:GetWaypoints()

local Folder = Instance.new("Folder")
Folder.Name = "Waypoints"
Folder.Parent = workspace

for i, v in ipairs(waypoints) do
	local Part = Instance.new("Part")
	Part.Position = v.Position
	Part.Anchored = true
	Part.Size = Vector3.new(1, 1, 1)
	Part.Parent = Folder
	if (i > 1) then
		local lastWaypoint = waypoints[i - 1]
		local dist = (v.Position - lastWaypoint.Position).Magnitude
		
		local tween = TweenService:Create(startpoint, TweenInfoNew, {Position = lastWaypoint.Position})
		tween:Play()
		
		local Part2 = Instance.new("Part")
		Part2.Anchored = true
		Part2.BrickColor = BrickColor.Black()
		Part2.Size = Vector3.new(.5, .5, dist)
		Part2.CFrame = CFrame.new(lastWaypoint.Position, v.Position) * CFrame.new(0, 0, dist / -2)
		Part2.Parent = Folder
	end
end