AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to limit a command to usage every x minutes (https://forums.alliedmods.net/showthread.php?t=302884)

blood2k 11-14-2017 10:29

How to limit a command to usage every x minutes
 
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

fysiks 11-14-2017 21:27

Re: How to limit a command to usage every x minutes
 
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.

blood2k 11-15-2017 02:44

Re: How to limit a command to usage every x minutes
 
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.
}


KiLLeR. 11-15-2017 05:36

Re: How to limit a command to usage every x minutes
 
Quote:

Originally Posted by fysiks (Post 2560547)
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.

jimaway 11-15-2017 08:47

Re: How to limit a command to usage every x minutes
 
change the last_used array to an integer if you want the command get blocked globally for everyone

fysiks 11-15-2017 09:27

Re: How to limit a command to usage every x minutes
 
Quote:

Originally Posted by KiLLeR. (Post 2560585)
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).

blood2k 11-15-2017 12:20

Re: How to limit a command to usage every x minutes
 
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

KiLLeR. 11-15-2017 12:31

Re: How to limit a command to usage every x minutes
 
Quote:

Originally Posted by fysiks (Post 2560615)
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 (Post 2560645)
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.

blood2k 11-15-2017 13:09

Re: How to limit a command to usage every x minutes
 
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

HamletEagle 11-15-2017 15:05

Re: How to limit a command to usage every x minutes
 
Remove [33] and [id].


All times are GMT -4. The time now is 08:52.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.