Multiple parts between two positions

Hello I have been trying to figure out how to put parts between two position such as this:

So Far I have gotten this but I do not the parts to go outside the two positions:

This is what I have done in the script

local partA = game.Workspace:WaitForChild("PointA")
local partB = script.Parent
local Folder = game.Workspace:WaitForChild("Lightning Prototype")
local Ray_Folder = game.Workspace:WaitForChild("Visualize the Ray")

local new_Params = RaycastParams.new()
new_Params.FilterDescendantsInstances = {partA, Folder, Ray_Folder}
new_Params.FilterType = Enum.RaycastFilterType.Blacklist
local origin = partA.CFrame.p
local direction = (partB.CFrame.p - origin)
local Make_Ray = workspace:Raycast(origin, direction, new_Params)

if Make_Ray then
	print(Make_Ray)
	print(Make_Ray.Instance)
	print(Make_Ray.Position)
	print(Make_Ray.Material)
	--print("true")
else
	Make_Ray = {}
	Make_Ray.Position = origin + direction
end

LRandom = math.random(1,10)
local Twodivise_Distance = (Make_Ray.Position + origin)/2--(Make_Ray.Position - origin).unit/LRandom

for make_parts = 1, LRandom do
	print("parts".. make_parts)
local Divise_Random = math.random(1, 10)
print(Divise_Random)
local Divise_Distance = Twodivise_Distance+(Make_Ray.Position - origin)/Divise_Random--[[(Twodivise_Distance-(Make_Ray.Position + origin))/Divise_Random]]
local Distance = (origin - Divise_Distance).Magnitude
local Visualize = Instance.new("Part", Ray_Folder)
Visualize.Name = ("Visualize Ray".. make_parts)
Visualize.Size = Vector3.new(0.75,0.75,0.75
Visualize.CFrame = CFrame.new(Divise_Distance)

Are you trying to multiple parts in a linear line?

That first picture looks a lot like a bezier curve. A picture looking at it from the origin to the finish would help out a lot in determining how you would go about it.

From the look of it, they’re staggered or organized in some way that’s not perfectly linear, so I don’t think so.

1 Like

Yes you are correct I am trying to put multiple parts between the origin (green part) and ending (red part).

What do you mean by that since you can see the origin and finish, green and red part.

Is this what you’re looking for?

If so, here is the place file for it Points.rbxl (21.7 KB)

Script:

local function CreatePoints(startPos, endPos, segments)
	local model = Instance.new("Model")
	model.Name = "Points"
	model.Parent = workspace
	
	for i = 0, segments do
		local offset = Vector3.new(math.random(-1, 1), math.random(-1, 1), math.random(-1,1))
		local newPos = startPos + (endPos - startPos).Unit * i * (endPos - startPos).Magnitude / segments
		
		if i == 0 or i == segments then
			offset = Vector3.new(0, 0, 0)
		end
		
		local point = game.ReplicatedStorage.Point:Clone()
		point.Position = newPos + offset
		point.Parent = model
	end
end

local startPart = workspace.Start
local endPart = workspace.End

CreatePoints(startPart.Position, endPart.Position, 10) --Last argument is the amount of points you want
3 Likes

Could you explain how it works since it looks interesting if you can?

Yea sure. First there is a function that creates the points. It takes in a start position and an end position which are both Vector3 values. The 3rd parameter is the amount of segments which you can think of as the amount of points.

Next, there is just a model to store all the parts. After that there is a for loop which will loop from 0 to however many segments there are. This will create a point each iteration. The offset makes it so the points are placed randomly. Without this, it will just be a straight line of points that are spaced evenly.

Now the math. We start by getting the start position and adding

(endPos - startPos).Unit

This creates a 1 stud vector that is “facing” the end position. Adding this creates a line that is positioned at the start position and faces the end position. We next multiply this vector by ‘i’ which is the index of the loop. This is what moves the point forward. Since we start at the number 0, multiplying a number by 0 is always 0 which places it right on the start position. The bigger the number, the farther forward it gets.

(endPos - startPos).Magnitude / segments

This section first gets the vector length of the two points using Magnitude. This is the distance between the two points. We divide this by the number of segments so the part is spaced evenly.

if i == 0 or i == segments then
    offset = Vector3.new(0, 0, 0)
end

This is an if statement that checks if the current part is the first part being made, or the last part. We need to check this so the parts are centered in the first and last part. You can see in the picture that both the green and red points have a part placed in them. Overall, it just removes the offset if it’s the first or last iteration.

Lastly, we position the part and add the offset to make it a random line of points.

1 Like

One more question how would I connect it if you could explain it??

Are you trying to make lightning by any chance?

I see that you are trying to make lightning. There are a couple tutorials you can follow such as these:

2 Likes

The finished product:

3 Likes

Thank you so much dude, thanks to this I’m now able to create my raycast lightning script I owe you everything