Need Help With Fixing Spam

So I have a sword tool and everytime the player clicks they receive coins. Is there a way to make it so the clicks do not spam and I can have a downtime. More specfically I’m asking is there a way to make it so that the player can not spam click, and the player can only click and get coins every second or so. Heres the clicking script.

Clicking Script

script.Parent.Activated:Connect(function()
	game.Workspace.MainEvent.AddCoins:FireServer()
	script.Parent.Enabled = false
	wait(1)
	script.Parent.Enabled = true
end)

Main Event Script

script.AddCoins.OnServerEvent:Connect(function(player)
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
end)

There is a remote event in Main Event called AddCoins

Use debounce it will probably help

local db = false
-- inside the coins giving part
if db == false then
    db = true
    wait(5)
    db = false
end

should I put this into the script?

1 Like

its not working
:frowning:
I don’t see an error code though

1 Like

local touched 
script.Parent.Activated:Connect(function()
    if not touched then touched = true
    workspace.MainEvent.AddCoins:FireServer()
	wait(3) -- cooldown
    touched = false
end)

A debounce mechanism.

Try using ReplicatedStorage for containing Remotes, seems better organized.

It would be safer to add a Debounce from the Server

it doesn’t work don’t know what the problem is

You would have to change the clicking script a bit. This should work.

local cooldown = false

script.Parent.Activated:Connect(function()
if cooldown == false then
cooldown = true
game.Workspace.MainEvent.AddCoins:FireServer()
wait (1) -- Change this to the amount you want the cooldown length to be.
cooldown = false
end
end)

What this is doing is basically when the player clicks, It checks if cooldown is equal to false. If it is, It will set it to true, Add the coins, and wait a second before it goes back to false so it works again.

Hoped this helped! :grinning:

2 Likes