Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make all models and parts within a model transparent?

Kind of difficult to understand. I tried to go with the ROBLOX version of their recursion with a slight edit to make it loop again but it only seems to be going through it once and only capture the open parts already and nothing inside the models. Can anyone hint me on how to fix this?

function onTouch(object)
	for _, child in ipairs(script.Parent.Parent.Parent.Parent.Bought.Room1:GetChildren()) do
		if child:IsA("BasePart") then
			while child.Transparency ~= 1 do
				child.Transparency = 1
				print(child.Name)
				wait()	
			end	
			wait()
		elseif child:IsA("Model") then
			onTouch(child)
			print(child.Name.."2")
			wait()
		end
	end
end
 
script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

The problem is that you are always searching through the same set of children every time.

Because the Touched event doesn't give you the object you want to make transparent as an argument, it will probably be best to create a new function for the recursion. This is because we need to recursive function's parameters to have some meaning in terms of what should be going on in the function.

I've named the function setChildrenTransparency and it takes an object for the inital parent and a number for the value you want to set the transparency to. When we go through the function the first time it will loop through the children in object setting each child's transparency to the given value if it inherits from BasePart. For each child, we also want to run the function again except this we want to examine the children of child. So now we pass child in as the parent object and it runs again.

This process keeps going until every single descendant of the original parent has been checked.

local function setChildrenTransparency(object, value)
    for _, child in ipairs(object:GetChildren()) do
        if child:IsA("BasePart") then
            child.Transparency = value
        end
        setChildrenTransparency(child, value)
    end
end

To put this function to work in your Touched event, we can use it like this:

local model -- Model to make transparent

script.Parent.Touched:connect(function(hit)
    setChildrenTransparency(model, 1)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.