Why does this cause lag and how to fix it?

I have recently added a X-Ray button which makes every single part in the game transparent, except for the players, when it’s turned on it causes a framerate drop, when it’s turned off the framerate goes back to normal, why does this happen and how to fix it?

Code (local script btw):

local isactivated = false

script.Parent.MouseButton1Click:Connect(function()
	if isactivated == false then
		isactivated = true
		for _,v in pairs(game.Workspace:GetDescendants()) do
			if v:IsA("Part") or v:IsA("UnionOperation") or v:IsA("MeshPart") or v:IsA("CornerWedgePart") or v:IsA("WedgePart") then
				if not v.Parent:FindFirstChild("Humanoid") and not v.Parent:IsA("Accessory") then
					if not v:FindFirstChild("PastTransparency") then
						local pasttransparency = Instance.new("NumberValue")
						pasttransparency.Name = "PastTransparency"
						pasttransparency.Value = v.Transparency
						pasttransparency.Parent = v
					end
					v.Transparency = 0.75
				end
			end
		end
	else
		isactivated = false
		for _,v in pairs(game.Workspace:GetDescendants()) do
			if v:IsA("Part") or v:IsA("UnionOperation") or v:IsA("MeshPart") or v:IsA("CornerWedgePart") or v:IsA("WedgePart") then
				if not v.Parent:FindFirstChild("Humanoid") and not v.Parent:IsA("Accessory") then
					if v:FindFirstChild("PastTransparency") then
						v.Transparency = v.PastTransparency.Value 
					end
				end
			end
		end
	end
end)

Does the frame rate drop every time they click the mouse button? Try activating the X-Ray mode but then walk around WITHOUT clicking your mouse button and see if its still laggy.

1 Like

What you mean by that is finding a way to trigger it without the mouseclick and see if that fixes the lag issue?

Well thats what I think is the problem. But to make sure can you just walk around in X-Ray mode without clicking your mouse button and see if you’re still laggy? Because if not, then its probably that.

1 Like

I just tried it out and no, the mouse click is not the problem

Some extra info:
When i haven’t activated X-Ray at all, i get around 40 fps, 70 ping and 20ms of frametime
When i have it activated, i get around 7 fps, 500 ping and 120ms of frametime
When i deactivate it, i get 40 fps, 70 ping and 20ms of frametime again.

Might be because now you’re rendering the whole map instead of just what you can see

2 Likes

Partially transparent parts cause lag. The lag will grow exponentially when you have a lot of transparent parts. This is because the game needs to calculate the color, material, reflectance, shadows, and so forth for the first part, and the second part behind it, and the one behind that, and behind that. If the part is invisible, the game doesn’t even try to process it, and if the part is opaque, the game can stop after it calculates the first one. This might not work out as well as you want in other words.

4 Likes

Oh, thanks for the explanation, is there any other way for me to do this without causing lag though?

Maybe instead of the whole map, only make the parts close to the player transparent. Or you could make an X-Ray gun that makes parts that it hits transparent.

Semi-opaque parts take a lot more processing power. Think about it, previous angles where light could not pass now can pass, do that to every part and every shadow and lighting has to be recalculated.

3 Likes

Now that you say it, it makes a lot of sense, never really thought of it that way.

1 Like
local isactivated = false

script.Parent.MouseButton1Click:Connect(function()
    isactivated = not isactivated
    for _,v in pairs(game.Workspace:GetDescendants()) do
        if v:IsA("BasePart") then
            if not v.Parent:FindFirstChild("Humanoid") and not v.Parent:IsA("Accessory") then
                if not v:FindFirstChild("PastTransparency") then
                    if isactivated then
                        local pasttransparency = Instance.new("NumberValue")
                        pasttransparency.Name = "PastTransparency"
                        pasttransparency.Value = v.Transparency
                        pasttransparency.Parent = v
                    else
                        v.Transparency = v.PastTransparency.Value
                    end
                end
                v.Transparency = isactivated and 0.75 or v.Transparency
            end
        end
    end
end)

please note that this WILL NOT fix the lag
I only did this to stop repeated code

Alternate solution (?)

Could you cover every limb of each player with SurfaceGUIs and set its AlwaysOnTop property to true? That way you’d see it through any parts in the game world, assuming you wanted to make it so you can see any player through stuff.

3 Likes

Exactly what i wanted! I forgot that AlwaysOnTop existed so i thought making every part transparent would be a good way to do it, thanks for the help!

2 Likes