Fix stacking bug

while wait(0.025) do
	if workspace.Trees.treecap.Value < 100 then
		workspace.Trees.treecap.Value = workspace.Trees.treecap.Value + 1
		newtree = game.ServerStorage.tree:Clone()
		newtree.Parent = game.Workspace.Trees
		newtree:MoveTo(Vector3.new(math.random(-103,252),6.9,math.random(-250,252)))
		newtree:PivotTo(newtree:GetPivot() * CFrame.Angles(0,math.random(0,360),0))
	end
end

this is script generating trees across the map in random position
sometimes trees may stack on each other


how do i fix that

1 Like

Use raycasting.

local function SpawnTree()
        workspace.Trees.treecap.Value = workspace.Trees.treecap.Value + 1
		newtree = game.ServerStorage.tree:Clone()
		newtree.Parent = workspace.Trees

        local Position = Vector3.new(math.random(-103,252),6.9,math.random(-250,252))
        local Direction = Vector3.new(0, -100, 0)

        local Result = workspace:Raycast(Position, Direction)
        if (Result and Result.Instance and Result.Instance.Parent == workspace.Trees) then return SpawnTree() end
		newtree:MoveTo()
		newtree:PivotTo(newtree:GetPivot() * CFrame.Angles(0,math.random(0,360),0))
        return newtree
end

while wait(0.025) do
	if workspace.Trees.treecap.Value < 100 then
		SpawnTree()
	end
end
1 Like

should 11 line

		newtree:MoveTo()

be

		newtree:MoveTo(Position)

Oh, yeah. Forgot about that when I moved it.

after a bit of experiementing i got into this script

local function SpawnTree()
	workspace.Trees.treecap.Value = workspace.Trees.treecap.Value + 1
	local newtree = game.ServerStorage.tree:Clone()
	newtree.Parent = workspace.Trees

	local Position = Vector3.new(math.random(-103,252),6.9,math.random(-250,252))
	local Direction = Vector3.new(Position.X, -100, Position.Z)

	local Result = workspace:Raycast(Position, Direction)
	if Result.Instance.Parent.Parent == workspace.Trees then return SpawnTree() end
	newtree:MoveTo(Position)
	newtree:PivotTo(newtree:GetPivot() * CFrame.Angles(0,math.random(0,360),0))
	return newtree
end

while wait(0.025) do
	if workspace.Trees.treecap.Value < 100 then
		SpawnTree()
	end
end

*I did a wide hitbox for trees, so raycasting works better
*changed direction into position math random
Still works 50/50 times
can you help?

fixed the problem i run onto, thanks for helping

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