Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I remove an object on second click?

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

✓ Accepted answer

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!

6 comments

0

It crashes my game...

0

It works fine for me.

0

Edited to show exactly what I tested.

2

Nevermind. It's because I didn't name it.

0

Nice. Glad it worked out!

0

Instead of using game.Workspace:findFirstChild("DeletionTarget"), you just make the brick created be destroyed.

This discussion is preserved as it was. New answers, comments, and votes are closed.