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

How to counter spam-clicking from players?

When scripting, I start to run into the same problem where when I call functions from clicks and I need to wait before running the code, I only want the current function to run (for example, if the user spam clicks and multiple functions are called, I only want the most recent function to run, cancelling the previous ones).

Let's say when clicked, I wanted a text button's text size to turn 30, wait(5), then turn back to it's original size, 14.

textButton.MouseButton1Click:Connect(function()
	textButton.TextSize = 30
	wait(5)
	textButton.TextSize = 14
end)

The problem is if the user only waited 4 seconds, then clicked it again, it wouldn't wait another 5 seconds, it would turn the size into 14 after 1 second because the previous function is still running. I want to make it so if a function is still running when a new one is called, the function that was running gets cancelled. This way if the user spam clicks, only the last click would count. It's kind of the opposite of a debounce if you think about it, I don't want only the first click to count, but the last one.

I've tried maaaaaaany different if-statements, and I've resolved this problem in the past using funky stuff, but I know there's definitely an easier way and I'm probably over-thinking it.

5 comments

0

Have a counter. Add one to it on each click.

0

Could you explain a little further?

0

I thinkin you also could use a debounce variable set true when your code run and wintg and the nset to false again

0

so you want the function to restart after the player clicks it again?

0

Yes, so if there were previous of the same functions called before then those would get cancelled. Basically a restart.

2 answers

✓ Accepted answer

This is the correct way to do it:

local i = 0 --Index i 

function lastClick()
	local v = 0 --Indexing v
	v = i --Set v equal to i
	wait(5)
	if v == i then --If v is still equal to i after 5 seconds, then...
		textButton.TextSize = 14 --Set text size equal to 14
	else --If not, then...
		return --Nevermind, forget it. Let the next execution worry about what happens next.
	end
end

textButton.MouseButton1Click:Connect(function() 
	textButton.TextSize = 30 --Sets text size equal to 30
	i = i + 1 --Increases i by 1 for each click
	lastClick() 
end)

1 comment

0

Much thanks! The v and i variables were a smart thing to do. :D

Try this:

local Cancel = false 
local ClickCount = 0

textButton.MouseButton1Click:Connect(function()
	if ClickCount == 0 then -- this if statement will determine to cancel the function on the second click
		ClickCount = 1
		Cancel = false 

	elseif ClickCount == 1 then 
		ClickCount = 0
		Cancel = true 
	end

	textButton.TextSize = 30

	for i = 0, 5, 1 do -- basically wait(5) but broken down to segments (like wait(1) wait(1) etc.)
		if Cancel then 
			textButton.TextSize = 30
			return
		end
		wait(1)
	end
	textButton.TextSize = 14
end)

1 comment

0

That doesn't look like it's gonna work

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