How to start removing created parts after a certain threshold

I’m currently trying to add in a decal limit to my game.

I’ve noticed recently that if there are a lot of decals on the map, the game begins to lose frames slightly. I’m wondering if there’s a way to start removing decals after a certain threshold is met. I’m not entirely sure how I would go about doing something like this. It would act similarly to the now deprecated Debris Service “MaxItems” limit. As much as I would like to use that service, there’s no way to group things into different categories, so sounds & ragdolls aren’t instantly removed if the limit is reached by decals, and the function is also deprecated.

Thanks in advance,
TheRings0fSaturn.

Might you be looking for CollectionService ?

1 Like

Yeah u could tag em and make ur own psuedo debris service with gc cycles.

1 Like

This seems the most likely, but how would I implement this? Currently playing around with it a bit.

Would you like to remove oldest decals or if it’s meets a certain criteria? I can write up some code now quick then I’ll have to go for tonight.

It would remove old decals, while allowing new decals to spawn in.

An example being you have four decals named W, X, Y, and Z.

A new decal is spawned that is named AA, and the maximum limit of decals is 4.

When that decal is spawned, Decal W is supposed to destroy itself, so on and so fourth. If decal AB is spawned in, then Decal X is destroyed.

I’m currently on mobile so I’m sorry for any obvious errors. Here is some code that just doesn’t insert decals after a certain number.
ServerScriptService:

local MaximumDecals = 128
game:GetService("Workspace").DescendantAdded:Connect(function(Object)
    if Object:IsA("Decal") then -- Add any other checks here.
        if #game:GetService("CollectionService"):GetTagged("Decal") >= MaximumDecals then
            Object:Destroy()
        end
    else
        game:GetService("CollectionService"):AddTag(Object, "Decal")
    end
end)

I just saw your post as I finished typing this on mobile which takes forever, hopefully you get the idea though.

1 Like

Going off Fractured’s script without using CollectionService

Not recommended if you want anything else to interact with the decals, but you could always add them to CollectionService through this anyway if you just want them all gone after say the end of a round, or set up a BindableEvent to return or clear the decal table.

local DecalTable = {}
local MaximumDecals = 128

workspace.DescendantAdded:Connect(function(Object)
	if Object:IsA(“Decal”) then –-Add any other checks here.
		if #DecalTable > MaximumDecals then
			table.remove(DecalTable,1)
			if DecalTable[i] then
				DecalTable[1]:Destroy()
			end
		end
		table.insert(DecalTable,Object)
	end)
end)
3 Likes

I’d probably recommend using a local table too as it means that there is one less tag to keep track of which won’t be used else where in the game, making the tagging system with CollectionService sort of pointless in a sense (still would work just a great though!).

This is the best solution that I have seen so far.

It appears though that the CollectionService didn’t even have to be used in this instance (I think.)

Still though, thanks to everyone who helped out here. :slight_smile:

One problem though:
It removes the latest decal instead of the first.


Also, what do you guys think a good default maximum decal limit is? I’m thinking it could be 2048 or 4096. Players could always change it to a higher or lower value with no limit really. Not entirely sure what the default should be though!