Trying to make a button where once pressed, a part disapears then once pressed again the part re-appears

Alright, trying to make a script where this button makes this part, visbile. once after pressed once you can press it a second time and then it will turn the transparency to 0. I’ve tried making this script many times and had failed many times.

I’ll try any suggestions and support. :smile:

Here is a method you can try:

First, add a RemoteEvent into ReplicatedStorage called “Toggle”. Then, in a LocalScript inside of your button, write the following code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toggle = ReplicatedStorage:WaitForChild("Toggle")

script.Parent.MouseButton1Click:Connect(function()
    toggle:FireServer()
end)

Then, in a ServerScript inside of the part, write the following code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toggle = ReplicatedStorage:WaitForChild("Toggle")

local toggled = false

toggle.OnServerEvent:Connect(function(player)
    toggled = not toggled

    if toggled then
        script.Parent.Transparency = 1
    else
        script.Parent.Transparency = 0
    end
end)

How is it supposed to locate the part?

The part is seperate, from the button. They aren’t connected although I can make the in the same if necessary.

It uses a RemoteEvent, and when fired the ServerScript will work with the part.

That’s confusing to me, upon pressing the button following everything you’ve said it still denys to work…

The question is how the click detection is made. ClickDetector allows it to be purely server-sided without doubt. Your question is about a toggle, it shouldn’t be harder than this:

local Part -- the part

ClickDetector.MouseClick:Connect(function()
    Part.Transparency = Part.Transparency == 0 and 1 or 0 -- woah cool way to utilizing conditions!
end)
3 Likes

Apparently, whenever I try make the re-appear version of it it doesn’t appear.

local Part = game.Workspace.samms

local ClickDetector = script.Parent.ClickDetector – the part

ClickDetector.MouseClick:Connect(function()

Part.Transparency = Part.Transparency == 1

end)

Close one works.

The script I initially sent handled both case of appearing and disappearing.

Did not work the first time I tried.

Was there any errors in the output console? If you did not find any errors, that would very strange. Otherwise, there’s something about the declaring of variables.

there’s something about the declaring of variables. Was what had occured upon trying to press the button.

hence why i added the variables, myself. (local part = game.Workspace.sammms, local ClickDetector = script.Parent.ClickDetector)

Can you elaborate what happened exactly? If it only worked once, I believe that’s a number issue, so here’s an alternative:

local Part -- the part
local invisible

ClickDetector.MouseClick:Connect(function()
    Part.Transparency = invisible and 0 or 1 -- woah cool way to utilizing conditions!
    invisible = not invisible
end)

Oh, apparently you haven’t assigned the variables correctly. You need to reference the variable to a part in order for it to work and the ClickDetector to the correct instance.

1 Like

Worked it out.

local Part = game.Workspace.samms – the part

local invisible

local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function()

Part.Transparency = invisible and 0 or 1 – woah cool way to utilizing conditions!

invisible = not invisible

end)

That seemed to work, thank you.

1 Like