Tree Clone Script not working

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

1 Like

could it be that a tree spawner is on the position of a tree leaf?

does the model have a primary part?

1 Like

Sorry I forgot to mention, there is one tree in ServerStorage that gets cloned 50 times whenever someone joins the game.

Yep, its the log.

1 Like

I think it may be the a CFrame of the weld, since that would also affect the position

1 Like

Can you explain? I’ve never heard of welds having CFrames.

1 Like

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
2 Likes

I tried this but now all of the trees go in the same place.

1 Like

Did they used to not go to the same place?

2 Likes

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
1 Like

Try this:

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.

1 Like

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
1 Like

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

1 Like

Did it work? Or did you just set my reply as the solution?

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.