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
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)
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