Pressing A Button To Make A Model Appear/Disappear

I’m trying to make it so if you hit a UI button, it makes a model show up. This is my current script for my button.

local Challenge = workspace.Challenge.num1
local trueorfalse = false
local Button = script.Parent
Button.MouseButton1Up:Connect(function()
if trueorfalse == false then
for _, Descendant in ipairs(Challenge:GetDescendants()) do
if Descendant:IsA(“BasePart”) then
Descendant.Transparency = 0
Descendant.CanCollide = true

elseif trueorfalse == true then
	for _, Descendant in ipairs(Challenge:GetDescendants()) do
		if Descendant:IsA("BasePart") then
					Descendant.Transparency = 1
					Descendant.CanCollide = false
		end
	end
end
	end
end

end)

Please properly format your code. You haven’t explained what’s wrong either

local Challenge = workspace.Challenge.num1
local trueorfalse = false
local Button = script.Parent
Button.MouseButton1Up:Connect(function()
	if trueorfalse == false then
		for _, Descendant in ipairs(Challenge:GetDescendants()) do
			if Descendant:IsA("BasePart") then
				Descendant.Transparency = 0
				Descendant.CanCollide = true
			elseif trueorfalse == true then
				for _, Descendant in ipairs(Challenge:GetDescendants()) do
					if Descendant:IsA("BasePart") then
						Descendant.Transparency = 1
						Descendant.CanCollide = false
					end
				end
			end
		end
	end
end)

Sorry, I’m a bit wonky right now, I’m really tired.

I just need to fix the code a bit to get it working, So I’ll just delete this post.

Your issue ultimately lines in the elseif clause of your inner if-statement. That was meant for the first if-statement, but the control structures were not properly closed. This is a better script:

local ModelToggleButton = script.Parent
local Model             = --[[Path.to.Model]]

local modelVisible = false


local function onActivated()
	modelVisible = not modelVisible
	
	local transparency = modelVisible and 0 or 1
	local canCollide   = modelVisible
	
	for _, descendant in Model:GetDescendants() do
		if not descendant:IsA("BasePart") then
			continue
		end
		
		descendant.Transparency = transparency
		descendant.CanCollide   = canCollide
	end
end


ModelToggleButton.Activated:Connect(onActivated)

You could also just temporarily set its parent to nil

1 Like

As the guy said, format your code before posting it.

My eyes hurt…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.