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?


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
			local Tree = TreeModel
			local TreeClone = Tree:Clone()
			TreeClone.Parent = game.Workspace.GameTrees
			TreeClone:PivotTo(, border.MaxX), math.random(border.MinY, border.MaxY), math.random(border.MinZ,border.MaxZ)))
			local distX = 0
			local distZ = 0
			local bool = true
				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)
					print("dist X:" .. distX)
					print("dist Z:" .. distZ)
					if distX < DistanceBetweenTrees and distZ < DistanceBetweenTrees then
						--TreeClone:PivotTo(, border.MaxX), math.random(border.MinY, border.MaxY), math.random(border.MinZ,border.MaxZ)))
						bool = false
				if not bool then
					TreeClone:PivotTo(, border.MaxX), math.random(border.MinY, border.MaxY), math.random(border.MinZ,border.MaxZ)))
			until(distX > DistanceBetweenTrees and distZ > DistanceBetweenTrees)

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: