Timer which allows users to perform one action per hour

Hello forum!

I am trying to code a timer, which allows users to donate only once per hour (to prevent spam and trolls).
Can someone help?

Thanks

Did you try yourself or do you need inspiration for how to do it?

I’m not quite sure my brain can grasp this… what exactly is spamming when they donate? Or are you talking about they just keep constantly typing public chat commands in?

[editline]19th October 2015[/editline]

Anyway, you could try a timer… never done one for an hour… so not sure if there’s going to be any complications (ie: something happening in that hour that breaks the timer and stops it). Probably your best bet is to setup a timer like this:




ply.DonateCooldown = CurTime() + 3600



Then check if whenever they try !donate (assuming that’s what you’re wanting it for), for the time since they last did a successful one.




if ( ply.DonateCooldown or 0 ) > CurTime() then
  // some failed print message here
   return
end



Then you’d do your successful code if the timer has not expired.

If you want to go gmod timer lua style - look at http://wiki.garrysmod.com/page/timer/Create

Sorry, was a bit confusing.

Basically I want to prevent users from constantly donating (I coded an automatic donation system).
You can donate with paysafecards only.
Imagine a troll comes, types random numbers and presses “Donate” 38x times.
I don’t like that in my database.

Users should only press “Donate” ONE TIME per hour, not as much as they want.

So… you created a automatic donation system. You mean via your website? And you want to link it with Lua? Or you coded something in Lua for your donations? In that case, you did that but don’t know how to setup a timer? You need provide more information here because I’m sort of even wondering if we’re talking about Lua or something like PHP. If it’s Lua, then use my advice above. If the timer hasn’t expired, then just SetVisible(false) on the button. Then set it to true when the timer is at 0.

I am very sorry for the missing info, my problem is the following:

I coded a Lua ADS (Auto-Donation-System) which only accepts paysafecards.
It’s not possible to check whether the user enters a valid paysafecard (there’s actually no paysafecard API for Lua).

Alright, I know the users **could ** enter invalid information (e.g. random numbers).
That’s nothing I can stop.

But I could restrict the max amount of donations, so why not code a timer which allows users to only donate one time in one hour.

Thanks for the coding example!

You coded something as complex as a donation system, but you cant figure out how to code a cooldown… Okay… Maybe you can make use of their steamID?

:snip: Nevermind

When they donate check if their PData value named “donationexample” or whatever(just keep it same everywhere) + 3600 > os.time() then an hour has passed since last time they used it. It’s best to use PData instead of just a variable on the player, because then if they would rejoin they could donate before the hour has passed. Keep in mind to same the current os.time() in their PData “donationexample” after they use the donation.

That’s true and thanks for the tip, I could check if the SteamID was recently added or so.

Thanks for the advice, I appreciate it!

I really do not believe you coded a donation system without knowing about timers.

Oh, I actually did not know about timers and coded a donation system
Forgot to say, the “issue” is fixed.
A simple timer, which runs once after a donation.
Wasn’t that hard, lol.




-- canDonate variable
local canDonate = true
function foo()
--blabla
...
DonateButton.DoClick() = function()

--blabla
canDonate = false
timer.Create("DonateCooldownTimer", 3600, 1, function()
canDonate = true
end)
end
--blabla
end


then set up a web server with a php script that does it and use http funcs in lua?

Why are you doing this clientside?