Raised This Month: $64 Target: $400
 16% 

How to limit a command to usage every x minutes


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
blood2k
Senior Member
Join Date: Mar 2014
Old 11-14-2017 , 11:29   How to limit a command to usage every x minutes
Reply With Quote #1

Could somebody share an example of limiting a client chat command to only once every minute?
Trying to figure this out with and without a timer... w.e is easier.

Something like if somebody uses = /health it would be blocked from usage by everyone in the server for a a minute with a message like, you must wait x amount b4 you can use this command again
blood2k is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-14-2017 , 22:27   Re: How to limit a command to usage every x minutes
Reply With Quote #2

Store get_systime() in a global variable each time the command is executed. Before actually executing the command, check if get_systime() minus the stored time is greater than 60 (seconds). If it is, execute the command, otherwise, don't execute the command and optionally print a message explaining why the command wasn't executed.

This is quite a simple concept so I'd recommend that you try it out yourself. If you can't get it to work after trying to write the code yourself, the code that you tried and we can help you fix it.

P.S. threads in the "Code Snippets/Tutorials" forum, are either code snippets or tutorials, not requests for anything. Since you are looking for scripting help, you should post this in Scripting Help. I reported the post so maybe a moderator will move it for you.
__________________
fysiks is offline
blood2k
Senior Member
Join Date: Mar 2014
Old 11-15-2017 , 03:44   Re: How to limit a command to usage every x minutes
Reply With Quote #3

Sorry for the wrong section.

So well.. I managed to put a working version of it up I just copy and paste things around here lol.. but it's only limiting it for the player who typed the command.. was thinking the first person to type it.. is the one that executes it and then it would be blocked for 2 minutes for everyone else. It's looking like this:


How to make it just block the command from being used during that cooldown period, with a message saying "You must wait X, amount before you can advertise again"
Code:
#include <amxmodx>
#include <amxmisc>

#define TIMER_TASK 32490283099

#define COOLDOWN 2 //minutes
new last_used[33]



public plugin_init()
{
	
	register_plugin("test", "1.0", "test")

	register_clcmd("say /advertise", "check", 0, "- show message");
	
}

public client_disconnected(id) {
    last_used[id] = 0;
}
public client_connect(id) {
    last_used[id] = 0;
}

public check(id) {
    if( (get_systime() - last_used[id]) < COOLDOWN*60) {
        client_print(id,print_chat,"[AMXX] You must wait %d minute%s to use this command again.",COOLDOWN, COOLDOWN == 1 ? "" : "s")
        return PLUGIN_HANDLED
    }
    last_used[id] = get_systime()
    set_task(1.0, "advertise")
    return PLUGIN_HANDLED
}

public advertise(id) {
   // Some function here that would be executed ONCE! then limited.
   // maybe... client.print an advertisement.. etc.
}

Last edited by blood2k; 11-15-2017 at 03:46.
blood2k is offline
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 11-15-2017 , 06:36   Re: How to limit a command to usage every x minutes
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
Store get_systime() in a global variable each time the command is executed. Before actually executing the command, check if get_systime() minus the stored time is greater than 60 (seconds). If it is, execute the command, otherwise, don't execute the command and optionally print a message explaining why the command wasn't executed.
Read it again.

Last edited by KiLLeR.; 11-15-2017 at 06:36.
KiLLeR. is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 11-15-2017 , 09:47   Re: How to limit a command to usage every x minutes
Reply With Quote #5

change the last_used array to an integer if you want the command get blocked globally for everyone
jimaway is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-15-2017 , 10:27   Re: How to limit a command to usage every x minutes
Reply With Quote #6

Quote:
Originally Posted by KiLLeR. View Post
Read it again.
He did it correctly with "less than" because he's using the reverse logic from how I stated it (i.e. he's denying the the use of the function if the elapsed time is less than the required time).
__________________
fysiks is offline
blood2k
Senior Member
Join Date: Mar 2014
Old 11-15-2017 , 13:20   Re: How to limit a command to usage every x minutes
Reply With Quote #7

Yeah, I thought it was done the way fysiks mentioned. I honestly look around on google, and the amxx wiki to figure out how to get any of this to work as I don't really know much about it..

But I'm still stuck with making it block EVERYONE.

Some1 mentioned up to turn the last_used array into an integer

not really sure how I'd do this as I'm total noob at this stuff lol
blood2k is offline
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 11-15-2017 , 13:31   Re: How to limit a command to usage every x minutes
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
He did it correctly with "less than" because he's using the reverse logic from how I stated it (i.e. he's denying the the use of the function if the elapsed time is less than the required time).
Ohh, yeah, I didn't notice that.

Quote:
Originally Posted by blood2k View Post
Yeah, I thought it was done the way fysiks mentioned. I honestly look around on google, and the amxx wiki to figure out how to get any of this to work as I don't really know much about it..

But I'm still stuck with making it block EVERYONE.

Some1 mentioned up to turn the last_used array into an integer

not really sure how I'd do this as I'm total noob at this stuff lol
last_used is array of integers and it is fine, since as I understand you want every player to be able to execute it once per 2 minutes, right?! Also this must work, I don't see any problems with it.

Last edited by KiLLeR.; 11-15-2017 at 13:35.
KiLLeR. is offline
blood2k
Senior Member
Join Date: Mar 2014
Old 11-15-2017 , 14:09   Re: How to limit a command to usage every x minutes
Reply With Quote #9

Yeah it works perfectly but what it's doing is only limiting the player that wrote it. So everyone can write it.. which can spam server. Would be nice to just let it execute once every x amount and be blocked for anyone to use for that time period
blood2k is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-15-2017 , 16:05   Re: How to limit a command to usage every x minutes
Reply With Quote #10

Remove [33] and [id].
__________________
HamletEagle is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:59.


Powered by vBulletin®
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Theme made by Freecode