I am making a script where trees and rocks are supposed to spawn in. The script works fine when generating trees, but when it’s time to generate rocks, it decides to just not clone them. I don’t know why the code doesn’t run for rocks since the rock model has pretty much the same things as the tree model, but it just doesn’t run. Using print() works in the function, everything else just doesn’t seem to run. I have also tried commenting out the :SetPrimaryPartCFrame() to see if positioning was a problem, but that wasn’t the case. I don’t know why it’s not cloning since the trees work fine.
- GenerationScript
local function CreateObject(objectName,FolderName,DestinationFolder)
local pos = (TileCount*TileSize)/2
local Angle = math.random(0,360)
local object = FolderName:WaitForChild(objectName):Clone()
object.Parent = DestinationFolder
object.PrimaryPart.Anchored = true
object:SetPrimaryPartCFrame(CFrame.new(math.random(pos*-1,pos),90,math.random(pos*-1,pos)))
local function SpecialObjects()
if object.Name == "Tree" then
object.TreeScript.Enabled = true
treecount = treecount - 1
end
if object.Name == "Rock" then
rockcount = rockcount - 1
end
end
local raycastorigin = object.PrimaryPart.Position
local raycastDirection = Vector3.new(0,-300,0)
local raycastresult = game.Workspace:Raycast(raycastorigin,raycastDirection)
if raycastresult then
if raycastresult.Instance.Name == "Grass" then
object:SetPrimaryPartCFrame(CFrame.new((raycastresult.Position)+Vector3.new(0,object.PrimaryPart.Size.Y/2.05,0))*CFrame.Angles(0,math.rad(Angle),0))
SpecialObjects()
else
object:Destroy()
end
else
object:Destroy()
end
end
- Loop
local function generateObjects()
while treecount > 0 do
CreateObject("Tree",resourcesDepositfolder,mapResources)
CreateObject("Rock",resourcesDepositfolder,mapResources)
task.wait()
end
--I also tried rocks in a sperate loop but it still doesn't work
--I call this function later
end
– Screenshots from the explorer
I also tried searching for rocks in the explorer and they aren’t found anywhere.