Models not cloning

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


error2

I also tried searching for rocks in the explorer and they aren’t found anywhere.

2 Likes

Does your rock model have a set primaryPart? Im currently looking into your code and cant seem to find any visible errors.

Primaryparts are set for both models
(sorry I forgot to mention)

Okay, then, there might be a problem with the raycast. Try printing out raycast results, their instance and the instance’s name

1 Like

This is because your models are not using .Clone method.

Local Clone = Model.Clone

That is… not how you type it.

image

Sorry for the confusion, this code should work. Make sure to use the API service of Roblox, it is more reliable.

Roblox.API:CloneModel(Model,workspace)

I printed the results and it shows that the raycast is hitting the rock mesh. I tried to make some raycast parameters to ignore the rock model but they don’t seem to work.

Edit: I’m pretty sure I forgot to put in parameters in the ray cast I’ll do that when I get back