Hi, I have a script that clones trees as soon as the player joins and puts them in random locations. My problem is that the trees can spawn on the leaves of other trees, even though I have set them to only spawn at y: 0. I have already tried using a touched event, but it still results on trees being spawned on other trees.
Script:
local tree = game.ServerStorage.Storage.Parts:WaitForChild("Tree")
local num = 1
local numberOfTrees = 50 - 1 --First number is the number of trees being spawned
local part = workspace.Spawn.TreeSpawner --An invisible part that acts as a radius
local xRadius = part.Size.X/2
local zRadius = part.Size.Z/2
-----------------------------------------------------------------------------
local function cloning()
local clone = tree:Clone()
local woodManage = clone.Log.WoodManager
local weld = clone.Log.Welds.LogToBaseplate
clone.Parent = game.Workspace.Spawn
woodManage.Enabled = true
clone:MoveTo(Vector3.new(math.random(-xRadius , xRadius ) + part.Position.X, 0 + (part.Position.Y - 4.125), math.random(-zRadius , zRadius ) + part.Position.Z))
weld.Enabled = true
clone.Log.Touched:Connect(function(part)
if part == clone then
clone:Destroy()
cloning()
else return end
end)
end
-----------------------------------------------------------------------------
for i = 0, numberOfTrees, 1 do
cloning() print("clone".. num) --The print is just for debugging
num += 1
end
It’s because :MoveTo respects collisions and therefore puts your object on top.
Try using :PivotTo:
local tree = game.ServerStorage.Storage.Parts:WaitForChild("Tree")
local num = 1
local numberOfTrees = 50 - 1 --First number is the number of trees being spawned
local part = workspace.Spawn.TreeSpawner --An invisible part that acts as a radius
local xRadius = part.Size.X/2
local zRadius = part.Size.Z/2
-----------------------------------------------------------------------------
local function cloning()
local clone = tree:Clone()
clone:PivotTo(CFrame.new(Vector3.new(math.random(-xRadius , xRadius ) + part.Position.X, 0 + (part.Position.Y - 4.125), math.random(-zRadius , zRadius ) + part.Position.Z)))
clone.Parent = workspace.Spawn
local woodManage = clone.Log.WoodManager
woodManage.Enabled = true
local weld = clone.Log.Welds.LogToBaseplate
weld.Enabled = true
clone.Log.Touched:Connect(function(part)
if part == clone then
clone:Destroy()
cloning()
else return end
end)
end
-----------------------------------------------------------------------------
for i = 0, numberOfTrees, 1 do
cloning() print("clone".. num) --The print is just for debugging
num += 1
end
They used to be randomized all over a specific area. Now they don’t randomize at all.
Script:
local tree = game.ServerStorage.Storage.Parts:WaitForChild("Tree")
local num = 1
local numberOfTrees = 50 - 1 --First number is the number of trees being spawned
local part = workspace.Spawn.Tree.TreeSpawner --An invisible part that acts as a radius
local xRadius = part.Size.X/2
local zRadius = part.Size.Z/2
-----------------------------------------------------------------------------
local function cloning()
local clone = tree:Clone()
clone:PivotTo(CFrame.new(Vector3.new(math.random(-xRadius , xRadius ) + part.Position.X, 0 + (part.Position.Y - 4.125), math.random(-zRadius , zRadius ) + part.Position.Z)))
clone.Parent = workspace.Spawn
local woodManage = clone.Log.WoodManager
woodManage.Enabled = true
local weld = clone.Log.Welds.LogToBaseplate
weld.Enabled = true
clone.Log.Touched:Connect(function(part)
if part == clone then
clone:Destroy()
cloning()
else return end
end)
end
-----------------------------------------------------------------------------
for i = 0, numberOfTrees, 1 do
cloning() print("clone".. num) --The print is just for debugging
num += 1
end
local tree = game.ServerStorage.Storage.Parts:WaitForChild("Tree")
local num = 1
local numberOfTrees = 50 - 1 --First number is the number of trees being spawned
local part = workspace.Spawn.TreeSpawner --An invisible part that acts as a radius
local xRadius = part.Size.X/2
local zRadius = part.Size.Z/2
-----------------------------------------------------------------------------
local function cloning()
local clone = tree:Clone()
clone:PivotTo(CFrame.new(math.random(-xRadius, xRadius), 0 + (part.Position.Y - 4.125), math.random(-zRadius, zRadius)))
clone.Parent = workspace.Spawn
local woodManage = clone.Log.WoodManager
woodManage.Enabled = true
local weld = clone.Log.Welds.LogToBaseplate
weld.Enabled = true
clone.Log.Touched:Connect(function(part)
if part == clone then
clone:Destroy()
cloning()
end
end)
end
-----------------------------------------------------------------------------
for i = 0, numberOfTrees do
cloning()
print("clone".. num) --The print is just for debugging
num += 1
end
Same problem. The weird part is I made a print statement and the coordinates of each of the tree clones are randomized, but when I see where they are, they are in a stack; also happening to be where I put the tree in ServerStorage.
What does WoodManager do? It could be resetting the point the tree moved to.
Try this:
--//Services
local ServerStorage = game:GetService("ServerStorage")
--//Variables
local tree = ServerStorage.Storage.Parts:WaitForChild("Tree")
local part = workspace.Spawn.TreeSpawner --An invisible part that acts as a radius
--//Controls
local num = 1
local numberOfTrees = 50 - 1 --First number is the number of trees being spawned
local xRadius = part.Size.X/2
local zRadius = part.Size.Z/2
--//Functions
local function cloning()
local clone = tree:Clone()
local woodManage = clone.Log.WoodManager
woodManage.Enabled = true
clone:PivotTo(CFrame.new(math.random(-xRadius, xRadius), 0 + (part.Position.Y - 4.125), math.random(-zRadius, zRadius)))
clone.Parent = workspace.Spawn
local weld = clone.Log.Welds.LogToBaseplate
weld.Enabled = true
clone.Log.Touched:Connect(function(part)
if part == clone then
clone:Destroy()
cloning()
end
end)
end
for i = 0, numberOfTrees do
cloning()
print("clone".. num) --The print is just for debugging
num += 1
end
Wood manager is for the axe, I made the tree choppable and it just manages the wood collection and chopping of the tree. Also still the same problem. No errors either
I had to change the script, completely, but the pivot thingy did work.
Script:
--Since WoodManager is disabled this script clones it and reenables it
local tree = game.ServerStorage.Storage.Parts:WaitForChild("Tree")
local num = 1
local numberOfTrees = 50 - 1 --First number is the number of trees being spawned
local part = workspace.Spawn.Tree.TreeSpawner
local xRadius = part.Size.X/2
local zRadius = part.Size.Z/2
-----------------------------------------------------------------------------
local function cloning()
local clone = tree:Clone()
local woodManage = clone.Log.WoodManager
local weld = clone.Log.Welds.LogToBaseplate
clone.Parent = game.Workspace.Spawn
woodManage.Enabled = true --Reenables the script
clone:PivotTo(CFrame.new(math.random(-xRadius, xRadius) + part.Position.X, part.Position.Y - 0.937, math.random(-zRadius, zRadius) + part.Position.Z))
weld.Enabled = true
end
-----------------------------------------------------------------------------
for i = 0, numberOfTrees, 1 do --Loops it the amount of times that you want clones
cloning() print("clone".. num)
num += 1
end