Decal Hiding Script Optimization Needed

Hello, I’m working on a game that has a ton of decals and it causes a lot of lag, and I needed to hide them if they get too far. I made this script to combat this issue, but it tanks the FPS and I am wondering if there is any further optimization available for it.

local plr = game:GetService("Players").LocalPlayer
local runService = game:GetService("RunService")
local distance = 100

runService.RenderStepped:Connect(function(delta)
	local stuff = game.Workspace.GearWalls:GetDescendants()

	for _,v in pairs(stuff) do
		if string.find(v.Name,"Button_") then
			local plrDistance = plr:DistanceFromCharacter(v.Position)
			--print("is button")
			if v:FindFirstChildWhichIsA("Decal") then
				v:FindFirstChildWhichIsA("Decal").Transparency = math.round(math.clamp(plrDistance,0,distance)/distance)
			end
		end
	end
end)

Any help is appreciated.

It’d be useful to tell us what the purpose of this script actually is.

Firstly, if you know that game.Workspace.GearWalls:GetDescendants() will always return the same value (you are not expecting any new decals to be added), just store it as a variable outside of the scope of RenderStepped instead of iterating through every decal every time RenderStepped fires.

Secondly, you could use CollectionService to just tag every decal and iterate through that list instead.

Keep in mind that Transparent Decals on parts are terribly intensive performance-wise.