# Can't Calculate The Distance Between Models

Hello everyone,

I’ve been dealing with this problem for a couple of hours and I finally gave up.

The Problem : Well I created a model of a Tree and I want to spawn it around the border, well I managed to do that but I want to have some king of distance between the trees so that wouldn’t collide with each other.

can anyone help me with that?

Code:

``````local Border = {
MinX = -500,
MaxX = 500,

MinY = 30,
MaxY = 34.5,

MinZ = -500,
MaxZ = 500
}

local MaxTrees = 200
local TreeRespawnTimeMin = 10 --60
local TreeRespawnTimeMax = 20 --180
local DistanceBetweenTrees = 12

while true do
wait(0.1)--[[.random(RespawnTimeMin, RespawnTimeMax)]]
if #game.Workspace.GameTrees:GetChildren() < maxTrees then

--Models
local Tree = TreeModel
local TreeClone = Tree:Clone()

--Positioning
TreeClone.Parent = game.Workspace.GameTrees
TreeClone:PivotTo(CFrame.new(math.random(border.MinX, border.MaxX), math.random(border.MinY, border.MaxY), math.random(border.MinZ,border.MaxZ)))

--args
local distX = 0
local distZ = 0
local bool = true

repeat
wait(0.1)
for i,v in pairs(game.Workspace.GameTrees:GetChildren()) do

distX = math.abs(v.PrimaryPart.Position.X - TreeClone.PrimaryPart.Position.X)
distZ = math.abs(v.PrimaryPart.Position.Z - TreeClone.PrimaryPart.Position.Z)

wait(0.1)

print("----------------------------")
print("dist X:" .. distX)
print("dist Z:" .. distZ)
print("----------------------------")

if distX < DistanceBetweenTrees and distZ < DistanceBetweenTrees then
--TreeClone:PivotTo(CFrame.new(math.random(border.MinX, border.MaxX), math.random(border.MinY, border.MaxY), math.random(border.MinZ,border.MaxZ)))
bool = false
end

end

if not bool then
TreeClone:PivotTo(CFrame.new(math.random(border.MinX, border.MaxX), math.random(border.MinY, border.MaxY), math.random(border.MinZ,border.MaxZ)))
end

until(distX > DistanceBetweenTrees and distZ > DistanceBetweenTrees)

print(TreeClone:GetPivot())
end
end
``````

Hmm, this is a tricky one.
You could make a hitbox for the tree, basically just a big invisible part that resembles the size of the tree.
Now say that a tree spawns to close to the other tree, if its touching this hitbox, then delete the tree.

After a bit of searching, this topic might help you, Trees spawning inside eachother

Have you tried using Magnitude to get the distance between the primary parts of the trees? If not, you can do this, it will returns the length of a Vector3, so by subtracting the first position by the second, it will give us the length of the distance between the two:
`(POSITION2 - POSITION1).Magnitude`