So I'm trying to make a script that creates a random-colored SelectionBox when you click the brick, and then destroy the SelectionBox when clicked again. Any suggestions?
Here's what I have so far:
(The print() lines are just so I can make sure the script is working.)
local part = script.Parent
function onClicked()
local _box_ = Instance.new("SelectionBox",part)
_box_.Adornee = part
_box_.Color3 = Color3.new(math.random, math.random, math.random)
print("Selection Box Created")
end
wait()
function onClickedAgain(playerWhoClicked)
wait(part:WaitForChild("SelectionBox"))
part.SelectionBox:Destroy()
print("Selection Box Destroyed")
end
script.Parent.ClickDetector.MouseClick:connect(onClickedAgain)
wait()
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Also, I know I could just toggle the visability of the SelectionBox, but I'm trying to challenge myself.
Thanks in advance. :)

2 comments
It's not working... It keeps giving an error saying "SelectionBox is not a valid member of part." I tried adding another variable to determine if the SelectionBox existed, but the script kept destroying the SelectionBox as soon as it was created. I'm starting to think I may need two separate scripts...
Well, that was just an example of how it could look like. I've explained how it works and given a random example as I'm having no clue about your Workspace-