Making models transparent via script

I’m trying to create a system where a model disappears for a set period of time once clicked, but I’m puzzled on what line of code to use. I believe it has something to do with GetChildren or ‘in pairs do’.

Here’s the code I’ve written:

local Flashlight = game.ServerStorage.Lighter
local clickable = true

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if clickable then
		local Find = player.Backpack:FindFirstChild("Lighter")
		if not Find then
			clickable = false
			script.Parent.ClickDetector.MaxActivationDistance = 0		
			Flashlight:Clone().Parent = player.Backpack
			wait(30)
			script.Parent.ClickDetector.MaxActivationDistance = 10
			clickable = true
		end
	end
end)

And here’s a screenshot of all the parts in that Model:
Screenshot_13

If any of you could help me with finding a simple line of code to use to make every part in StaticLighter transparent, I would much appreciate it. Thanks!

2 Likes
local model = -- your model (script.Parent.Parent in your current case)

local descendants = model:GetDescendants() -- you can use :GetChildren() if there are not BaseParts parented to another BasePart

for i=1,#descendants do
    local descendant = descendants[i]
    if descendant:IsA("BasePart") then
        descendant.Transparency = 1
    end
end

20 Likes

Thank you - this code worked smoothly. I’ll keep that in mind for any future scripting.

1 Like

Hey, this code worked great as I need a model to disappear when I click on it. However, it seems that the decal on the model doesn’t turn transparent. Everything goes as scripted except the decal. Any idea on how to script a decal to become invisible?


Screen Shot 2021-01-19 at 9.46.19 PM

The decal isn’t a BasePart so it’s not turning transparent. You would need to change
if descendant:IsA("BasePart") then
in order to also have the decal become transparent.

1 Like
local model = -- your model (script.Parent.Parent in your current case)

local descendants = model:GetDescendants() -- you can use :GetChildren() if there are no BaseParts parented to another BasePart

for i=1,#descendants do
    local descendant = descendants[i]
    if descendant:IsA("BasePart") or descendant:IsA("Decal") then
        descendant.Transparency = 1
    end
end

Only difference is that you’re looking for the decals inside the model, along with baseparts “or descendant:IsA(“Decal”)”

2 Likes

Oh ok thank you this worked well! Thank you!

Thank you this worked great and will certainly use this for future models as well!