My script somehow won't clone parts randomly

the script:

local spawnArea = script.Parent -- Script should be inside the part that marks the area

while wait(0.5) do
	local clone = game.ReplicatedStorage:WaitForChild("Fuel"):Clone() -- Part you want to clone
	clone.Parent = game.Workspace -- Add it to the game
	
	local posX = math.random( -(spawnArea.Size.X / 2) ,(spawnArea.Size.X / 2) ) -- Gets the random X value
	local posY = (spawnArea.Size.Y / 2) + .5 
	local posZ = math.random( -(spawnArea.Size.Z / 2) , (spawnArea.Size.Z / 2) ) -- Gets the random Z value
	local pos = Vector3.new(posX,posY,posZ) -- Marks the final vector
	
	clone.CFrame = (spawnArea.CFrame * CFrame.new(pos)) -- Positions the part inside the spawnArea
	
end

the video:

only 1 spawned but there were supposed to be more that spawned.

1 Like

If it does, what does it say in the output?

1 Like

“CFrame is not a valid member of Model “Workspace.Fuel””

From looking at this you’ll need to use Model:SetPrimaryPartCFrame(CFrame_Here). You can do this by setting a primary part in the model. If the model only contains one part I suggest just using the part and taking it out of the model. Set the PrimaryPart to the Egg. This would only apply if what you are getting from ReplicatedStorage is a Model?

It’s a Model right?

its a model with 2 parts and a script in on part for money when it gets touched

So I would set the PrimaryPart in the model to the inner of outer part of the egg or whichever part you want to use to place the egg. Changing this part of the script to

local spawnArea = script.Parent -- Script should be inside the part that marks the area

while wait(0.5) do
	local clone = game.ReplicatedStorage:WaitForChild("Fuel"):Clone() -- Part you want to clone
	clone.Parent = game.Workspace -- Add it to the game

	local posX = math.random( -(spawnArea.Size.X / 2) ,(spawnArea.Size.X / 2) ) -- Gets the random X value
	local posY = (spawnArea.Size.Y / 2) + .5 
	local posZ = math.random( -(spawnArea.Size.Z / 2) , (spawnArea.Size.Z / 2) ) -- Gets the random Z value
	local pos = Vector3.new(posX,posY,posZ) -- Marks the final vector

	clone:SetPrimaryPartCFrame(spawnArea.CFrame * CFrame.new(pos)) -- Positions the part inside the spawnArea

end

should work.

it works!!! Thanks. But I have one more question… how do I set a limit so it stop at the max number I want it to stop at but then when the parts are gone/deleted it will resume?

1 Like

take for example i have 9 parts and the max is 10 so i take 1 and now its 8 parts but it keeps going till there are 10 but when there are 10 parts it stops cloning until there are less than 10

Add this. You may need to adjust some values as well as adding a folder into the workspace called "EggFolder" .

local max_parts = 10 -- max amount of parts
local spawnArea = script.Parent
local EggFolder = workspace.EggFolder

for z = 1,max_parts do
	local clone = game.ReplicatedStorage:WaitForChild("Fuel"):Clone() -- Part you want to clone
	clone.Parent = EggFolder -- Add it to the game

	local posX = math.random( -(spawnArea.Size.X / 2) ,(spawnArea.Size.X / 2) ) -- Gets the random X value
	local posY = (spawnArea.Size.Y / 2) + .5 
	local posZ = math.random( -(spawnArea.Size.Z / 2) , (spawnArea.Size.Z / 2) ) -- Gets the random Z value
	local pos = Vector3.new(posX,posY,posZ) -- Marks the final vector

	clone:SetPrimaryPartCFrame(spawnArea.CFrame * CFrame.new(pos)) -- Positions the part inside the spawnArea
end



EggFolder.ChildRemoved:Connect(function()
	wait(0.5)
	if #spawnArea:GetChildren() < max_parts then
		local clone = game.ReplicatedStorage:WaitForChild("Fuel"):Clone() -- Part you want to clone
		clone.Parent = EggFolder -- Add it to the game

		local posX = math.random( -(spawnArea.Size.X / 2) ,(spawnArea.Size.X / 2) ) -- Gets the random X value
		local posY = (spawnArea.Size.Y / 2) + .5 
		local posZ = math.random( -(spawnArea.Size.Z / 2) , (spawnArea.Size.Z / 2) ) -- Gets the random Z value
		local pos = Vector3.new(posX,posY,posZ) -- Marks the final vector

		clone:SetPrimaryPartCFrame(spawnArea.CFrame * CFrame.new(pos)) -- Positions the part inside the spawnArea
	end

end)

Any questions, ask away!

1 Like

hey can you fix this script im tryining to make is so when the player presses E then the fuel of the part goes up but the fuel the player has goes down.
script:


local player = game.Players.LocalPlayer

prox.Triggered:Connect(function(plr)
	local user = player --get player from touching human
	local stats = user:WaitForChild("leaderstats")
	
	local fuel = stats.Fuel.Value
	local orbfuel = game.Workspace.Orb.Orb.Fuel
	
	orbfuel.Value = orbfuel.Value + fuel.Value
	fuel.Value = 0
end)

error code: ( Workspace.Press E.ProximityPrompt.Script:7: attempt to index nil with ‘WaitForChild’)

I also had this problem recently but haven’t reported it to roblox yet.
you can still use Random.new() though.

May I ask if this script is a local script? To update leaderstats you should always do it from the server (you can use a remote event in this case)