Making all parts in model transparent (efficiently)

Hello everyone,

I am making my zombie framework and noticed that I had to not render some zombies when they were off-screen. However, the solution I made for it was not the best (the one in red):

image

Code:

   function RENDER:show()
		for _, part in pairs(self.own:GetChildren()) do
			if part.Name == "Head" or part:IsA("MeshPart") then 
				part.Transparency = 0
			end
		end
	end

As you can tell, I looped through all of the model to change its parts transparency according to its position on the screen. Are there better ways of changing the parts in a model more efficiently? Please let me know below. Thank you!

EDIT: I found a solution! Here is how it works:

  • save a table with all of the parts that need to be changed
  • loop through that table

Code:

   function RENDER:save_parts()
       local tab = {}
       for i, part in pairs(self.own:GetChildren()) do
			if part.Name == "Head" or part:IsA("MeshPart") then 
				tab[i] = part -- save the part to a table
			end
		end
    end

    function RENDER:hide()
		local things = self.parts -- get the parts
		
		for _, part in pairs(things) do
			local decal = part:FindFirstChild("decal")
			if part.Transparency == 1 then return end
			if decal then decal.Transparency = 1 end
			part.Transparency = 1
		end
		
	end
	
	function RENDER:show()
		local things = self.parts -- get the parts
		
		for _, part in pairs(things) do
			local decal = part:FindFirstChild("decal")
			if part.Transparency == 0 then return end
			if decal then decal.Transparency = 0 end
			part.Transparency = 0
		end
		
	end

Thank you for reading through his post, but I hope you also found this solution helpful.