I have example here, sorry for unredable code, i writed this a 2 years ago:
local folder = script.Parent
local PetsFolder = folder.Pets
local template = folder.Pet
local Player = folder.Player
local Count = folder.Count
local collumn = 3
local startPos = Player.Position + Vector3.new(2,0,2)
local spacing = 2
local function OnRemove()
wait(0)
for i, p in pairs(PetsFolder:GetChildren()) do
if p and p:IsA("BasePart") then
i-= 1
local x = i%collumn
local z = math.floor(i/collumn)
p.CFrame = Player.CFrame * CFrame.new(2,0,2) * CFrame.new(-x*spacing,0,z*spacing)
end
if i == #PetsFolder:GetChildren() then
break
end
end
end
local function OnAdded(object:BasePart)
for i = 0, #PetsFolder:GetChildren()-1 do
local x = i % collumn
local z = math.floor(i/collumn)
object.CFrame = Player.CFrame * CFrame.new(2,0,2) * CFrame.new(-x*spacing,0,z*spacing)
object.Parent.ChildRemoved:Connect(function(obj)
OnRemove()
end)
if i == #PetsFolder:GetChildren()-1 then
break
end
end
end
PetsFolder.ChildAdded:Connect(function(object)
OnAdded(object)
end)
local function OnAdd()
for i = 1, 3 do
template:Clone().Parent = PetsFolder
end
end
wait(0.5)
OnAdd()
or this:
local template = script.Template
local template2 = script.Template2
local new_seed = tick()
print(new_seed," seed")
local default_parameter = { -- parametry
name = "default";
offset = CFrame.identity;
parent = workspace;
temp = template;
temp2 = template2;
base_Size = Vector3.new(3,20,3); -- wielkośc
multiplier = 3; -- ilość powtórzeń generacji
split = 5; -- ilość gałęzi
seed = new_seed;
scale = Vector3.new(0.8,1,0.8); -- skala gałęzi, mnożnik wielkości
angle = math.pi/5; -- kąt rozchylenia
}
local counterGen = 1
local random = Random.new(default_parameter.seed or tick()) -- generacja losowej liczby
local function values(A,B) for i, v in pairs(B) do A[i] = v end end -- zmiana ustawień arraya
local function Main(parameter,option)
if option then values(parameter,option) end -- zmiana wartości
local tree = Instance.new("Model")
tree.Name = parameter.Name or "Tree"
tree.Parent = parameter.parent
local base = parameter.temp:Clone()
base.Name = "Base"
base.Size = parameter.base_Size
base.CFrame = script.Template.CFrame*CFrame.new(0,parameter.base_Size.Y/2,0)
base.Parent = tree
local level = {
[0]={base}
}
for i = 1, parameter.multiplier do
local new_level = {}
for j, branch in pairs(level[i-1]) do -- branch is last level part, soo the new branches are branches on new level, soo position set to it is the part that ends last level
for k = 1, parameter.split do
local rot_Y = CFrame.Angles(0,(random:NextNumber()-0.25)*math.pi*2,0) -- using random numbers and multiplying by Tau to get random angle
local new_Angle = CFrame.Angles(parameter.angle,0,0) -- Getting Angle up from pi
local new_branch = parameter.temp:Clone()
new_branch.Name = "Branch"
local sized_new = branch.Size * parameter.scale
new_branch.Parent = tree -- przypisanie parenta gałęzi
new_branch.Size = sized_new -- nowa wielkość
new_branch.CFrame = branch.CFrame * CFrame.new(0,branch.Size.Y/2,0)*rot_Y*new_Angle*CFrame.new(0,sized_new.Y/2,0) -- offseting and rotating branch
--
table.insert(new_level,new_branch) -- przypisanie gałęzi do tabeli
local leveled = level[parameter.multiplier-1] -- checks if this is last level
if leveled then
local leaves = parameter.temp2:Clone()
leaves.Name = "Leaves"
local sizes = Vector3.new(sized_new.X*7,sized_new.Y/2,sized_new.Z*7)
leaves.Size = sizes
leaves.Parent = tree
leaves.CFrame = new_branch.CFrame * CFrame.new(0,sizes.Y/7+(new_branch.Size.Y/2)-sized_new.Y/5,0)
end
wait(0)
if k == parameter.split then
break
end
end
wait(0)
if j == #level[i-1] then
break
end
end
table.insert(level,new_level) -- making new levels
end
end
for i = 1,counterGen do
Main(default_parameter,{offset = CFrame.new(0,10,0)})
wait(0.5)
end
NOTE: This script was 40% of youtube, i remade it and analyse it to make it work better
or:
local step = 2
local start = workspace.Start.Position
local brick = workspace.Brick
local function Round(n:number,m:number,s:number):number
return (math.floor(n/m+0.5)*m)+s
end
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.TargetFilter = brick
local function GetSystem(n:number,m:number)
local a = (math.floor(n/m+0.5)*m)
local b = math.floor(a/2)
local c = b%2
return c
end
mouse.Move:Connect(function()
local pos = mouse.Hit.Position
local X = Round(pos.X,step,start.X)
local Z = Round(pos.Z,step,start.Z)
local x = math.floor(X-pos.X)
local z = math.floor(Z-pos.Z)
local n = X-Round(x,step,0)
local m = Z-Round(z,step,0)
local i = workspace.Start.Size.X
local v = workspace.Start.Size.Z
local a = GetSystem(i,step)
local b = GetSystem(v,step)
if a == 0 then
n = n + brick.Size.X/2
end
if b == 0 then
m = m + brick.Size.Z/2
end
brick.Position = Vector3.new(n,12,m)
end)