For scripting examples for learning how to use AMXX.
Archive-Poster
Posts: 1347
Gender:
Archive-Poster
Profile
Joined: Tue Aug 04, 2026 2:18 am
Gender:
Post
by Archive-Poster » Fri Sep 25, 2026 4:11 am
User AFK status
This plugin has two natives that can be used to determine how long player is AFK or set his AFK time.
I made this plugin a long time ago, faced strange problems creating it, so some parts of the code may look a bit strange, but it works as it should. I know there are other method(s) to do this, but someone may still find this usefull. I know I did.
Natives
get_user_afktime(id) - returns player's AFK time.
set_user_afktime(id, Float:time) - Sets player's AFK time.
Usage
Code: Select all
/*
* Returns user's inactivity time.
*
* @param id Player index.
*
* @return Time in seconds (Float).
*
* Example:
* new Float:afkTime = get_user_afktime(id)
*/
native Float:get_user_afktime(id)
/*
* Sets user's inactivity time.
*
* @param id Player index.
* @param Float:value Time in seconds (Float).
*
* Example:
* set_user_afktime(id, 15.0)
*/
native set_user_afktime(id, Float:value)
Attachments
user_afk_status.inc.zip
(452 Bytes) Downloaded 5 times
user_afk_status.sma.zip
(1.7 KiB) Downloaded 4 times
Archive-Poster
Posts: 1347
Gender:
Archive-Poster
Profile
Joined: Tue Aug 04, 2026 2:18 am
Gender:
Post
by Archive-Poster » Fri Sep 25, 2026 4:13 am
Example plugin
AFK kicker + AFK sprite.
Code: Select all
#include <amxmodx>
#include <engine>
#include <user_afk_status>
#define AFK_SPRITE // comment to disable afk sprite
new Float:Freq = 1.0
const Immunity = ADMIN_LEVEL_H
new g_pMaxAfkTime
#if defined AFK_SPRITE
new g_pMinAfkTime, g_AfkSprite
public plugin_precache()
g_AfkSprite = precache_model("sprites/afkstatus.spr")
#endif
public plugin_init()
{
register_plugin("Simple AFK kicker + AFK sprite", "1.0", "beast")
#if defined AFK_SPRITE
g_pMinAfkTime = register_cvar("min_afk_time", "5")
Freq = 0.5
#endif
g_pMaxAfkTime = register_cvar("max_afk_time", "120")
register_think("AfkTimer", "CheckAfkPlayers")
new ent
do
ent = create_entity("info_null")
while(!is_valid_ent(ent))
entity_set_string(ent, EV_SZ_classname, "AfkTimer")
entity_set_float(ent, EV_FL_nextthink, get_gametime() + Freq)
}
public CheckAfkPlayers(ent)
{
static players[32], num, i, id, Float:maxAfkTime, Float:afkTime
get_players(players, num, "a")
#if defined AFK_SPRITE
static Float:minAfkTime
minAfkTime = get_pcvar_float(g_pMinAfkTime)
#endif
maxAfkTime = get_pcvar_float(g_pMaxAfkTime)
static name[32]
for(i = 0; i < num; i++)
{
id = players[i]
afkTime = get_user_afktime(id)
#if defined AFK_SPRITE
if(afkTime >= minAfkTime)
{
if(is_user_alive(id))
{
message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
write_byte(TE_PLAYERATTACHMENT)
write_byte(id)
write_coord(40)
write_short(g_AfkSprite)
write_short(floatround(Freq * 10))
message_end()
}
}
#endif
if(get_user_flags(id) & Immunity)
continue
if(afkTime >= maxAfkTime)
{
get_user_name(id, name, charsmax(name))
server_cmd("kick #%d You were AFK longer than %.1f seconds", get_user_userid(id), maxAfkTime)
client_print(0, print_chat, "[AFK Kicker] %s was kicked for being AFK longer than %.1f seconds", name, maxAfkTime)
log_amx("%s was kicked for being AFK longer than %.1f seconds", name, maxAfkTime)
}
}
entity_set_float(ent, EV_FL_nextthink, get_gametime() + Freq)
}
Attachments
afkstatus_sprite.zip
(267 Bytes) Downloaded 5 times
Users browsing this forum: No registered users and 1 guest