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 make a script wait between clicks?

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. :)

1 answer

✓ Accepted answer

You could use a variable that's being set true after the first click and false after the second one to show that it has been clicked the 1st/2nd time. I'll show you what I mean:

clickvar = false
script.Parent.ClickDetector.MouseClick:connect(function(plr) -- No clue whether you need the plr var
	if clickvar == false then
		if not script.Parent.SelectionBox then
			_box = Instance.new("SelectionBox", script.Parent)
			_box_.Adornee = part
			_box_.Color3 = Color3.new(math.random, math.random, math.random)
			clickvar = true
		elseif script.Parent.SelectionBox then
			repeat script.Parent.SelectionBox:Destroy() wait() until not script.Parent.SelectionBox
			_box = Instance.new("SelectionBox", script.Parent)
			_box_.Adornee = part
			_box_.Color3 = Color3.new(math.random, math.random, math.random)
			clickvar = true
		end
	elseif clickvar == true then
		if script.Parent.SelectionBox then
			repeat script.Parent.SelectionBox:Destroy() wait() until not script.Parent.SelectionBox
			clickvar = false
		else
			clickvar = false
		end
	end
end)

This will pretty much check whether the "clickvar" variable is set true or false. If it's set true then any SelectionBox will be destroyed. If it's set false then first any SelectionBox will be destroyed and then a new one will be created.

2 comments

0

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...

0

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-

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