OnServerEvent being called twice on second try

So i’m making a skip vote system that will skip a wave if a specific amount of people vote to skip. The problem is when I vote to skip for the second time, the Current Votes add up 2 times and idk why. I tried adding a debounce when the server is fired, I tried adding a debounce when recieving the event. Still the same problem. Here’s the script:

ReplicatedStorage.Events.RecieveVote.OnServerEvent:Connect(function()
			local debounce = false
			if not debounce and not waveSkipped then
				debounce = true
				print("Added 1 Current Vote")
				CurrentVotes += 1
				ReplicatedStorage.Events.SkipVote:FireAllClients(CurrentVotes, NeededVotes)
				wait(1)
				debounce = false
			end
		end)

Try that and tell me if worked.

local debounceEvent = Instance.new("BindableEvent")

ReplicatedStorage.Events.RecieveVote.OnServerEvent:Connect(function()
	if not debounceEvent.Event:IsScheduled() and not waveSkipped then
		debounceEvent.Event:Fire()
		print("Added 1 Current Vote")
		CurrentVotes += 1
		ReplicatedStorage.Events.SkipVote:FireAllClients(CurrentVotes, NeededVotes)
		debounceEvent:Wait(1)
	end
end)
1 Like

To explain what the issue here is:
A debounce is (as you probably know) meant to prevent spamming. If you define the variable debounce inside the event-connection then it will only exist within that specific “catch”, which means it won’t provide spamming.

Defining it outside, just as @hollaquetalBRUH did, will mean all event-“catches” will share one debounce. This can be made in a more beginner friendly way (I have actually never seen it done this way, it sure is interesting though!)

local debounce = false

ReplicatedStorage.Events.RecieveVote.OnServerEvent:Connect(function()
	if not debounce and not waveSkipped then
		debounce = true
		print("Added 1 Current Vote")
		CurrentVotes += 1
		ReplicatedStorage.Events.SkipVote:FireAllClients(CurrentVotes, NeededVotes)
		task.wait(1)
		debounce = false
	end
end)

BindableEvent better and more advanced :3

More advanced, yes. Better? Not entirely sure. I think BindableEvents require more memory than a boolean, and I don’t think :IsEventScheduled() is faster than if boolean (since it must call a function).

The performance and memory difference should however be negligible anyways, but the BindableEvent is more expensive than a primitive boolean

You can do a lot of things more with just 1, everybody have preferences =>
BindableEvent | Roblox Creator Documentation

I get an error “IsScheduled is not a valid member of RBXScriptSignal”

Still prints out 2 times when I vote for the second time

Yes, everybody has preferences which is why I referred to it as an interesting solution, because I genuinely think it is a cool solution I haven’t seen before. Bindableevents are great, and can be used for a lot, but I don’t think calling it a better solution in this case is correct

Try changing

if not debounce and not waveSkipped then
    ...
end

to

if (not debounce) and (not waveSkipped) then
    ...
end
-- OR (these two are equivalent)
if not (debounce or waveSkipped) then
    ...
end

Still print’s out 2 times when I vote for the second time