Placing an amount of Parts between 2 other Parts

Hi,

I wanted to ask for help about placing a specific amount of Parts between 2 other Parts.
Here are pictures that might help explaining it:

Let’s say that there are 2 Parts like this:

The line is supposed to say that those 2 parts create a straight line where I want to place some other Parts (the line is not really there it’s just for the picture.)

I want to place the Parts like this and I also want to decide how many Parts there should be.

I dont really know how to do something like this so I was thinking about asking for help. Can someone please explain this to me?

Thanks to anyone who helps me.

local Part1 = workspace:FindFirstChild(“Part1”)
local Part2 = workspace:FindFirstChild(“Part2”)
local PlacedPart = game:GetService(“ServerStorage”):FindFirstChild(“PlacedPart”)

local ManyParts = 20

local Distance = (Part1.Position - Part2.Position).Magnitude
local Debris = Instance.new(“Folder”)
Debris.Name = “Debris”
Debris.Parent = workspace

for i =1,ManyParts do
local DistanceWise = Distance/ManyParts*i
local CloneP = PlacedPart:Clone()
CloneP.CFrame = Part1.CFrame * CFrame.new(0,0,Distance) * CFrame.new(0,0,-DistanceWise)
CloneP.Parent = Debris

end

1 Like

This is simple, what you gonna do is to call a specific amount of parts you want to place and the distance between parts :

local part1 = game.Workspace.Part1 -- replace this with your start point part
local part2 = game.Workspace.Part2 -- replace this with your end point part
local amount = 5 -- replace this with how many parts you want between each parts
for i = 0, amount do
	if part1 and part2 then -- just to check
		local part = Instance.new("Part",workspace) -- create a new part in workspace
		part.Size = Vector3.new(1,1,1)
		part.Anchored = true 
		
		part1.CFrame = CFrame.new(part1.Position,part2.Position) -- i don't know why but without this line, the script doesn't work really well
		part2.CFrame = CFrame.new(part2.Position,part1.Position) -- same thing 
		
		local distance = (part1.Position - part2.Position).Magnitude -- gets the distance between those 2 parts
		local location = distance/amount -- get the location that the part should be to line up evenly with others part
		part.CFrame = part1.CFrame * CFrame.new(0,0,-location * i) -- idk, find out yourself
	end
end

However, i notice that the script i just wrote about kind of doesn’t create the parts perfectly and always missing once which is inside the end part, i can’t really fix this without making it harder and mess up, so always get your amount of parts you want and add one to it
here are some of the results :
Amount : 5 (It’s actually make 4 parts because what i said above)


Amount : 10 (It’s actually make 9 parts because what i said above

1 Like

Your script kinda work but


The part were placed in the wrong direction

I find this to be a good explanation:

The explanation is here, you can a unit vector going to the direction of another part (P2-P1).Unit and you scale it with the distance you want, the middle point for example would multiply this unit direction vector by the magnitude L divided by 2 as its in the middle.

image

Using CFrame.new() will generate this unit vector from the direction of part1 to part2 in the direction of the look vector or Z axis of a part. So this should work as well.

CFrame:Lerp is also another possible solution which also internally does this.

1 Like

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