Only one cactus moves at a time, and throws a lot of errors

So im trying to create the effect that something in my game is moving even though its not, so I decided to have it so cacti slowly move every frame, making it feel like the vehicle you are on is moving. My problem is that only one cacti move at a time and the output keeps on throwing errors about the cacti primary part not set even though it is

Code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Cactus = ReplicatedStorage:WaitForChild("Cactus")

function update()
	for i,v in pairs(workspace.CactusSpawns.CactusSpawn:GetDescendants()) do
		if v:IsA("Model") then
			print("model")
			if v:GetPrimaryPartCFrame().Z < -360 then
				v:Destroy()
			else
				v:SetPrimaryPartCFrame(v:GetPrimaryPartCFrame() - Vector3.new(0,0,0.25))
			end
		end
	end
end

RunService:BindToRenderStep("CactusMove", Enum.RenderPriority.Last.Value, update)

while wait(math.random(3,7)) do
	local Clone = Cactus:Clone()
	Clone.Parent = workspace.CactusSpawns.CactusSpawn
	
	Clone:SetPrimaryPartCFrame(workspace.CactusSpawns.CactusSpawn.CFrame + Vector3.new(math.random(-15, 15), 0,math.random(-15, 15)))
end

What are the exact errors? And can you put a picture of the Explorer pls?

It looks like the problem is GetDescendants(). This is going to also grab everything within each cactus. So if your cactus is made up of other models, and those models don’t have a PrimaryPart, you’ll get the error about the primary part not being set. You probably want to use GetChildren() instead.

1 Like

This completely fixed it, just by changing Descendants to Children :grinning:

1 Like