How can I change a part's Invisibility using a gui button?

So Im trying to make a gui button that can control two part’s invisibility at the same time. I want it so that when I click this button it makes a part invisible and the other visible. (And vice versa when clicked again).

This is what I have at the moment but It isnt working, how can I improve this code so that it works?

local button = script.Parent
local skirtpuff = game.Workspace.skirtpuff 
local skirtplain = game.Workspace.skirtpuff

local function onClick()
	if skirtpuff.Transparency == 0 then
		skirtpuff.Transparency = 1
		skirtplain.Transparency = 0
	else
		skirtpuff.Transparency = 0
		skirtplain.Transparency = 1
	end
end

button.MouseButton1Click:Connect(onClick)

1 Like

These reference the same object

oh thats a typo on my part writing the code on the forum, I just copied it from memory instead of copy pasting it. My bad!

I mean, other than what omega said… that’s about as simple as it gets

local button = script.Parent
local skirtpuff = workspace.skirtpuff 
local skirtplain = workspace.skirtplain

local function onClick()
	skirtpuff.Transparency = skirtpuff.Transparency == 1 and 0 or 1
	skirtplain.Transparency = skirtplain.Transparency == 1 and 0 or 1
end

button.MouseButton1Click:Connect(onClick)

Shouldn’t you use a RemoteEvent to make changes to Transparency on the server instead? This makes sure that every client can see the changes made to the part, not just the client that clicked the GUI button

That sounds perfect! How would I change my code to make it a remoteEvent? Im just getting by with my game with very little knowledge of Lua so Im very new to everything atm

Runtime Architecture: Learn more about how the client/server boundary works
Remote Documentation: Learn more about how they work, remote documentation & example usage

Create a RemoteEvent, put it into ReplicatedStorage, and create a variable in your LocalScript, and a server based script placed into ServerScriptService that accesses the RemoteEvent in ReplicatedStorage. Once you do that, run :FireClient() on the RemoteEvent that you defined in your LocalScript after the user clicks on the button. In your server script, create an event (OnServerEvent), which will run when :FireClient() is run from the client. Then, change the transparencies as you did previously in the server script

After some experimenting I figured it out. My code WAS working, it just apparently doesn’t like Meshparts… to work arround this i just made normal parts with the same names and put specialmeshes under them. I feel so dumb oh my god-

wondering if this code can work with entire models, hopefully it does!

It can! modifying the code a little bit I was able to make the code work with models. Using Plain1 and Puffy1 as the model names

wait(5)
local button = script.Parent
local Puffy1 = game:GetService("Players").LocalPlayer.Character.Torso:FindFirstChild("Puffy1")
local Plain1 = game:GetService("Players").LocalPlayer.Character.Torso:FindFirstChild("Plain1")

local function setTransparency(model, transparency)
	for _, part in ipairs(model:GetDescendants()) do
		if part:IsA("Part") then
			part.Transparency = transparency
		end
	end
end

local function onClick()
	if Puffy1 and Plain1 then
		local firstPartPuffy1 = Puffy1:FindFirstChildOfClass("Part")
		local firstPartPlain1 = Plain1:FindFirstChildOfClass("Part")

		if firstPartPuffy1 and firstPartPlain1 then
			if firstPartPuffy1.Transparency == 0 then
				setTransparency(Puffy1, 1)
				setTransparency(Plain1, 0)
			else
				setTransparency(Puffy1, 0)
				setTransparency(Plain1, 1)
			end
		end
	end
end

button.MouseButton1Click:Connect(onClick)