I have a tree-placing system that grabs already existing nodes and casts a ray from it onto the ground, spawning a bunch of trees:
local CS = game:GetService("CollectionService")
local SpawnTrees = game.ServerStorage.SetUpMap.SpawnTrees
local Trees = game.Workspace.Trees.HappyTrees:GetChildren()
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances = Trees
function SpawnTrees()
local Map = game.Workspace:WaitForChild("Map")
for _, Node in pairs(CS:GetTagged("TreePlacer")) do
wait()
local x = math.random(1,3)
if x == 1 or x == 2 then
local Result = game.Workspace:Raycast(Node.CFrame.Position, Node.CFrame.LookVector * 100, Params)
if Result then
local Tree = Trees[math.random(1, #Trees)]
local Clone = Tree:Clone()
Clone.Parent = game.Workspace
Clone:MoveTo(Result.Position)
end
end
Node:Destroy()
end
end
SpawnTrees()
This code works properly except for the fact that it decides tree placement in a very weird and inconsistent way.
Here are the nodes before spawning trees (they may be hard to see but they’re the little white dots):
Here are the trees after they’ve been placed:
You’ll notice that all of the nodes were recognized but the trees on the left didn’t spawn at all. The script destroyed all of the nodes and didn’t place a single tree.
Why does this happen?