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

Save button cooldown not working? (can just keep spamming the button and limit the datastores)

I made a save button to save your data when you click on it but I want it to have a cooldown since there is a limit in datastores.

I'm trying to make it so when you click on the save button it will have a 60 second cooldown and you will have to wait 60 seconds until you can save again.

Here's what I have so far..

local debounces = false
saveiconbutton.MouseButton1Click:connect(function()
	if debounces == false then
		local debounces = true
		savinglabel.Text = "Saving.." 
		savinglabel:TweenSize(UDim2.new(0, 200, 0, 50), "Out", "Quad", 0.5) -- this will pop up a textlabel telling you that your data is saving
		ds1:SetAsync(plr.UserId, moneyStats.Value)
		wait(5)
		savinglabel.Text = "Saving Success!"
		wait(2)
		savinglabel:TweenSize(UDim2.new(0, 0, 0, 0), "In", "Quad", 0.5)
	else
		for v = 60, 0, -1 do
		wait(1)
		if v > 1 then
			savinglabel.Text = "You can save in "..v.."seconds."--this will tell the play how much longer you have to wait to save again
			break
		elseif v < 1 then-- if the 60 seconds is up then it will make it so you can click the button again
			local debounces = false
					
		end
		end
	end
end)

But instead of just waiting 60 seconds, I can spam click the button and it would save each time.. Any idea how to fix it?

1 answer

✓ Accepted answer

This is because you are setting the debounce to false in the wrong place. Instead of putting the debounce = false in the if debounce == false, you put somewhere else which requires less than or more than checks which you can't just glance over and see what it does. You should place it once it is done saving. Here is what your code should look like.

local debounces = false
local lastsave = nil
saveiconbutton.MouseButton1Click:connect(function()
    if debounces == false then
       	debounces = true --cannot be local or else variable will only be defined this in the scope
	lastsave = math.floor(tick())
        savinglabel.Text = "Saving.." 
        savinglabel:TweenSize(UDim2.new(0, 200, 0, 50), "Out", "Quad", 0.5) -- this will pop up a textlabel telling you that your data is saving
        ds1:SetAsync(plr.UserId, moneyStats.Value)
        wait(5)
        savinglabel.Text = "Saving Success!"
        wait(2)
        savinglabel:TweenSize(UDim2.new(0, 0, 0, 0), "In", "Quad", 0.5)
	wait(53)
	debounces = false
    else
        savinglabel.Text = 'You can save in '..math.floor(tick()) - lastsave..' seconds.'
    end
end)

Hope this helps!

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