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.
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
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)
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.
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.