Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I remove an object on second click?

Asked by 9 years ago

This SurfaceGui makes a part when clicked. How do I make the part disappear when I click the Surface Gui again?

local Surface = Instance.new("SurfaceGui", script.Parent)
Surface.Adornee = script.Parent

local frame = Instance.new("Frame", Surface)
    frame.Size = UDim2.new(.8, 160, 1, 160)
    frame.Transparency = 1


local Button = Instance.new("TextButton",frame)
    Button.Size = UDim2.new(.8, 160, 0.58, 160)
    Button.BackgroundColor3 = BrickColor.White().Color
    Button.Text = "Brick Creator"
    Button.TextScaled = true 


Button.MouseButton1Down:connect(function()
    local brick Instance.new("Part", game.Workspace)

end)

1 answer

Log in to vote
1
Answered by
Chronomad 180
9 years ago

Try this. When ClickSwitcher = false, the part will be created upon clicking. When it = true, The part specified within the parenthesis will be destroyed.

    ClickSwitcher = false -- Acts as a sort of Debounce.
            Button.MouseButton1Down:connect(function()
            if ClickSwitcher  = false then 
            ClickSwitcher = true

    local brick Instance.new("Part", game.Workspace)
    brick.Name = ("DeletionTarget")

    elseif ClickSwitcher  ==  true then
    ClickSwitcher = false
    local brick2 = game.Workspace:FindFirstChild("DeletionTarget")
        brick2:Destroy()

Let me know if this helps!

0
It crashes my game... 64batsalex 45 — 9y
0
It works fine for me. Chronomad 180 — 9y
0
Edited to show exactly what I tested. Chronomad 180 — 9y
2
Nevermind. It's because I didn't name it. 64batsalex 45 — 9y
View all comments (2 more)
0
Nice. Glad it worked out! Chronomad 180 — 9y
0
Instead of using game.Workspace:findFirstChild("DeletionTarget"), you just make the brick created be destroyed. OneTruePain 191 — 9y
Ad

Answer this question