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.
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.
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)
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.
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!