Here is a full code BTW:
local TweenService = game:GetService("TweenService")
local RootList = {}
local heart = workspace.Overgrowth.OverGrowthHeart
table.insert(RootList, heart)
local function GetNeighbors(previousRoot)
local availableSpots = {}
local overlapParams = OverlapParams.new()
overlapParams.FilterDescendantsInstances = {workspace.Overgrowth:GetChildren(), workspace:GetChildren()}
overlapParams.FilterType = Enum.RaycastFilterType.Include
local UpCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(0, 1, 0), Vector3.new(0.8, 0.8, 0.8), overlapParams)
local DownCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(0, -1, 0), Vector3.new(0.8, 0.8, 0.8), overlapParams)
local LeftCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(-1, 0, 0), Vector3.new(0.8, 0.8, 0.8), overlapParams)
local RightCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(1, 0, 0), Vector3.new(0.8, 0.8, 0.8), overlapParams)
local FrontCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(0, 0, -1), Vector3.new(0.8, 0.8, 0.8), overlapParams)
local BackCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(0, 0, 1), Vector3.new(0.8, 0.8, 0.8), overlapParams)
if #UpCheckBoxResult == 0 then
table.insert(availableSpots, previousRoot.Position + Vector3.new(0, 1, 0))
end
if #DownCheckBoxResult == 0 then
table.insert(availableSpots, previousRoot.Position + Vector3.new(0, -1, 0))
end
if #LeftCheckBoxResult == 0 then
table.insert(availableSpots, previousRoot.Position + Vector3.new(-1, 0, 0))
end
if #RightCheckBoxResult == 0 then
table.insert(availableSpots, previousRoot.Position + Vector3.new(1, 0, 0))
end
if #FrontCheckBoxResult == 0 then
table.insert(availableSpots, previousRoot.Position + Vector3.new(0, 0, -1))
end
if #BackCheckBoxResult == 0 then
table.insert(availableSpots, previousRoot.Position * Vector3.new(0, 0, 1))
end
return availableSpots
end
local function GetBest(previousRoot, positionsArray)
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Include
overlapParams.FilterDescendantsInstances = {workspace.Structures:GetChildren(), workspace.Overgrowth:GetChildren()}
local valueList = {}
local connectionsList = {}
for i, pos in pairs(positionsArray) do
local connections = {}
local rightCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(1.5, 0, 0), Vector3.new(0.2, 0.8, 0.8), overlapParams)
local leftCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(-1.5, 0, 0), Vector3.new(0.2, 0.8, 0.8), overlapParams)
local upCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(0, 1.5, 0), Vector3.new(0.8, 0.2, 0.8), overlapParams)
local downCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(0, -1.5, 0), Vector3.new(0.8, 0.2, 0.8), overlapParams)
local backCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(0, 0, 1.5), Vector3.new(0.8, 0.8, 0.2), overlapParams)
local frontCheckBoxResult = workspace:GetPartBoundsInBox(previousRoot.CFrame * CFrame.new(0, 0, -1.5), Vector3.new(0.8, 0.8, 0.2), overlapParams)
local connectionValue = 0
if #rightCheckBoxResult > 0 then
connectionValue = connectionValue + 1
table.insert(connections, rightCheckBoxResult)
end
if #leftCheckBoxResult > 0 then
connectionValue = connectionValue
table.insert(connections, leftCheckBoxResult)
end
if #upCheckBoxResult > 0 then
connectionValue += 1
table.insert(connections, upCheckBoxResult)
end
if #downCheckBoxResult > 0 then
connectionValue += 1
table.insert(connections, downCheckBoxResult)
end
if #backCheckBoxResult > 0 then
connectionValue += 1
table.insert(connections, backCheckBoxResult)
end
if #frontCheckBoxResult > 0 then
connectionValue += 1
table.insert(connections, frontCheckBoxResult)
end
table.insert(valueList, connectionValue)
connectionsList[i] = connections
end
local bestValue = math.max(unpack(valueList))
local bestValueIndex = table.find(valueList, bestValue)
local bestOfTheBestValues = {}
local bestOfTheBestPositions = {}
local bestOfTheBestConnections = {}
for i, v in pairs(valueList) do
if v == bestValue then
table.insert(bestOfTheBestValues, v)
table.insert(bestOfTheBestPositions, positionsArray[bestValueIndex])
table.insert(bestOfTheBestConnections, connectionsList[bestValueIndex])
end
end
local nextPositionIndex = math.random(1, #bestOfTheBestValues)
local nextPosition = bestOfTheBestPositions[nextPositionIndex]
local nextConnection = bestOfTheBestConnections[nextPositionIndex]
local positionsAndConnections = {
[1] = nextPosition;
[2] = nextConnection
}
print(positionsAndConnections)
return positionsAndConnections
end
local function SpawnNextRoot(previousRoot, parameters)
local nextRootPosition = parameters[1]
local spawnPosition
if nextRootPosition == previousRoot.Position + Vector3.new(0, 1, 0) then
spawnPosition = nextRootPosition + Vector3.new(0, -0.5, 0)
elseif nextRootPosition == previousRoot.Position + Vector3.new(0, -1, 0) then
spawnPosition = nextRootPosition + Vector3.new(0, 0.5, 0)
elseif nextRootPosition == previousRoot.Position + Vector3.new(-1, 0, 0) then
spawnPosition = nextRootPosition + Vector3.new(0.5, 0, 0)
elseif nextRootPosition == previousRoot.Position + Vector3.new(1, 0, 0) then
spawnPosition = nextRootPosition + Vector3.new(-0.5, 0, 0)
elseif nextRootPosition == previousRoot.Position + Vector3.new(0, 0, -1) then
spawnPosition = nextRootPosition + Vector3.new(0, 0, 0.5)
elseif nextRootPosition == previousRoot.Position + Vector3.new(0, 0, 1) then
spawnPosition = nextRootPosition + Vector3.new(0, 0, -0.5)
local newRoot = Instance.new("Part")
newRoot.Anchored = false
newRoot.Size = Vector3.new(0.002, 0.002, 0.002)
newRoot.Position = spawnPosition
newRoot.Color = Color3.fromRGB(80, 109, 84)
newRoot.Parent = workspace.Overgrowth
newRoot.Name = tostring("root".. tostring(#RootList + 1))
for i, c in pairs(parameters[2][1]) do
local weld = Instance.new("WeldConstraint")
weld.Part0 = newRoot
weld.Part1 = c
weld.Parent = newRoot
end
local growthTweenGoals = {}
growthTweenGoals.Position = nextRootPosition
growthTweenGoals.Size = Vector3.new(1, 1, 1)
local growthTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0, false, 0)
local growthTween = TweenService:Create(newRoot, growthTweenInfo, growthTweenGoals)
growthTween:Play()
print(RootList)
else
print("smth went wrong")
end
end
script.Parent.MouseClick:Connect(function()
repeat
SpawnNextRoot((RootList[#RootList]), GetBest(RootList[#RootList], GetNeighbors(RootList[#RootList])))
task.wait(2)
until
#RootList == 25
end)