How to make Debris service create multiple parts?

I have a rock that will fall from the sky and crush the ground. When it crushes the ground, I want it to summon parts using debris service to simulate parts falling off of it.
But the problem is, I don’t know how to make debris service create more than one part, and with differing randomness from the part that is shown below.

Here’s a small snippet of code where I use the service:

		local DebrisService = game:GetService("Debris")
		local debris = Instance.new("Part", game.Workspace)
		debris.Position = Vector3.new(bomb.Position.X - math.random(2, 6) + math.random(2, 6), bomb.Position.Y + math.random(8, 12), bomb.Position.Z - math.random(2, 6) + math.random(2, 6))
		debris.Size = Vector3.new(math.random(1, 3), math.random(1, 3), math.random(1, 3))
		debris.Color = Color3.new(0.639216, 0.611765, 0.470588)
		DebrisService:AddItem(debris, 2)

Any help would be much appreciated!

I have a feeling you don’t know what Debris service does. The first parameter of Debris:AddItem() is to specify a part, and the second parameter is how long the part will last for until it’s destroyed.
All it does is just destroy a part after the specified time. It doesn’t create parts.

I already know that the 2nd paramater is to schedule deletion of the part. I want to know how to make it create multiple parts.

Oh, just put it in a for loop like this:

for i = 1, 10 do
	local debris = Instance.new("Part", workspace)
	debris.Position = Vector3.new(bomb.Position.X - math.random(2, 6) + math.random(2, 6), bomb.Position.Y + math.random(8, 12), bomb.Position.Z - math.random(2, 6) + math.random(2, 6))
	debris.Size = Vector3.new(math.random(1, 3), math.random(1, 3), math.random(1, 3))
	debris.Color = Color3.new(0.639216, 0.611765, 0.470588)
	
	task.delay(2, function()
		debris:Destroy()
	end)
end

Also, you shouldn’t use Debris as it uses the outdated wait(n) instead of task.wait(n). Use task.delay to replace it.

The benefit of Debris:AddItem(Instance, Duration) is that it doesn’t error when called on an already destroyed instance (unlike Destroy()).

You can just check if the instance is still there after:

task.delay(2, function()
	if debris and debris:IsDescendantOf(workspace) then
		debris:Destroy()
	end
end)

That is possible too, although you don’t need if debris as it has already been defined in the script. Destroying an instance doesn’t cause the variables which point to it to point to nil (they still point to the destroyed instance). On a slightly different note, 0 is a valid second argument for Debris:AddItem().

task.delay(2, function()
	Debris:AddItem(Part, 0)
end)
1 Like