SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 12/09/2018, 12:31 AM   #1
PoniStar
Big Clucker
 
Join Date: Aug 2018
Posts: 104
Reputation: 0
Default Prevent AFK Player Damage

Hi , How Can i prevent afk player damage ? i mean for exp when id 1 is on ESC [afk] another player can give him damage and when the id 1 get back he will die ! i dont want when players are AFK anyone can kill them
PoniStar is offline   Reply With Quote
Old 12/09/2018, 02:32 AM   #2
KinderClans
Banned
 
Join Date: May 2018
Location: Italy
Posts: 749
Reputation: 43
Default Re: Prevent AFK Player Damage

What? Wasn't the opposite? As I remember afk'ed players don't take damage.
KinderClans is offline   Reply With Quote
Old 12/09/2018, 08:38 AM   #3
DarkMythHunter
Big Clucker
 
Join Date: May 2017
Location: Philippines
Posts: 85
Reputation: 24
Default Re: Prevent AFK Player Damage

Quote:
Originally Posted by PoniStar View Post
Hi , How Can i prevent afk player damage ? i mean for exp when id 1 is on ESC [afk] another player can give him damage and when the id 1 get back he will die ! i dont want when players are AFK anyone can kill them
Quote:
Originally Posted by KinderClans View Post
What? Wasn't the opposite? As I remember afk'ed players don't take damage.

On some servers, example for survival ones if a player used ESC key to exit to the main menu rather than /afk and another player shot them if that player comes back he'll get the damage. He don't want that to happen...




Anyways, I suggest just use or make an /afk command using SetPlayerVirtualWorld then freeze them, and if they uses /back just unfreeze and set bis virtual world back to the original world.
DarkMythHunter is offline   Reply With Quote
Old 12/09/2018, 10:41 AM   #4
iLearner
Gangsta
 
iLearner's Avatar
 
Join Date: Apr 2017
Posts: 631
Reputation: 134
Default Re: Prevent AFK Player Damage

Use Emmet's include "new_callbacks.inc" that contains the function 'IsPlayerPaused', you can use it under onplayertakedamage to prevent the damage.

That's obviously easy solution but there are a lot of others as well.
iLearner is offline   Reply With Quote
Old 12/09/2018, 01:03 PM   #5
PoniStar
Big Clucker
 
Join Date: Aug 2018
Posts: 104
Reputation: 0
Default Re: Prevent AFK Player Damage

can you say what exacly i should write under onplayertakedamage ?
PoniStar is offline   Reply With Quote
Old 12/09/2018, 01:17 PM   #6
Bolex_
Gangsta
 
Bolex_'s Avatar
 
Join Date: Aug 2015
Location: Republic Of Croatia
Posts: 594
Reputation: 116
Default Re: Prevent AFK Player Damage

Code:
#if defined _OnPlayerPause_included
    #endinput
#endif
#define _OnPlayerPause_included

#include <a_samp>

#define MinTimeWithoutUpdates 6 // Time in seconds after which a player will be considered as paused.
//#define PRINTPAUSE // Define this if you want this script to print messages on your server console. This
//  is useful for example for admin filterscripts.

new stock TimeOfLastUpdate[MAX_PLAYERS];
new stock bool:Paused[MAX_PLAYERS];
new stock bool:InClassSelection[MAX_PLAYERS];

new afktime[MAX_PLAYERS];
//new Text3D:afklabel[MAX_PLAYERS];
stock IsPlayerPaused(playerid)
{
    if(Paused[playerid] == true)
    {
        return 1;
    }
    return 0;
}

stock IsPlayerInClassSelection(playerid)
{
    if(InClassSelection[playerid] == true)
    {
        return 1;
    }
    return 0;
}

#define MinTimeWithoutUpdates2 (MinTimeWithoutUpdates * 1000)

forward SearchForNewlyPausedPlayers();
public SearchForNewlyPausedPlayers()
{
    new CurrentTime, PlayerState;
    CurrentTime = GetTickCount();
    for(new i = 0; i < MAX_PLAYERS; i ++)
    {
        if(Paused[i] == false)
        // If the player is already paused, there's no point in calling OnPlayerPause again.
        {
            if(InClassSelection[i] == false)
            // When a player enters the class selection screen, he stops sending updates just
            //  like when he pauses, so we need to know if he's selecting class.
            {
                if(IsPlayerConnected(i))
                {
                    if( (CurrentTime - TimeOfLastUpdate[i]) >= MinTimeWithoutUpdates2 )
                    {
                        PlayerState = GetPlayerState(i);
                        if( (PlayerState == PLAYER_STATE_ONFOOT) || (PlayerState == PLAYER_STATE_DRIVER) || (PlayerState == PLAYER_STATE_PASSENGER) )
                        {
                            OnPlayerPause(i);
                        }
                    }
                }
            }
        }
    }
    return 1;
}

forward OnPlayerPause(playerid);
forward OnPlayerUnpause(playerid);

static gOnPlPa_HasCB[7];

public OnGameModeInit()
{
    SetTimer("SearchForNewlyPausedPlayers", 500, true);

    gOnPlPa_HasCB[0] = funcidx("OnPlPa_OnPlayerConnect") != -1;
    gOnPlPa_HasCB[1] = funcidx("OnPlPa_OnPlayerDisconnect") != -1;
    gOnPlPa_HasCB[2] = funcidx("OnPlPa_OnPlayerRequestClass") != -1;
    gOnPlPa_HasCB[3] = funcidx("OnPlPa_OnPlayerSpawn") != -1;
    gOnPlPa_HasCB[4] = funcidx("OnPlPa_OnPlayerUpdate") != -1;
    gOnPlPa_HasCB[5] = funcidx("OnPlPa_OnPlayerPause") != -1;
    gOnPlPa_HasCB[6] = funcidx("OnPlPa_OnPlayerUnpause") != -1;
    if (funcidx("OnPlPa_OnGameModeInit") != -1)
    {
        return CallLocalFunction("OnPlPa_OnGameModeInit", "");
    }

    return 1;
}

#if defined _ALS_OnGameModeInit
    #undef OnGameModeInit
#else
    #define _ALS_OnGameModeInit
#endif
#define OnGameModeInit OnPlPa_OnGameModeInit
forward OnPlPa_OnGameModeInit();

public OnPlayerConnect(playerid)
{
    Paused[playerid] = false;
    InClassSelection[playerid] = false;

    if (gOnPlPa_HasCB[0])
    {
        return CallLocalFunction("OnPlPa_OnPlayerConnect", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerConnect
    #undef OnPlayerConnect
#else
    #define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect OnPlPa_OnPlayerConnect
forward OnPlPa_OnPlayerConnect(playerid);

public OnPlayerDisconnect(playerid,  reason)
{
    Paused[playerid] = false;
    InClassSelection[playerid] = false;

    if (gOnPlPa_HasCB[1])
    {
        return CallLocalFunction("OnPlPa_OnPlayerDisconnect", "ii",playerid,  reason);
    }
    return 1;
}
#if defined _ALS_OnPlayerDisconnect
    #undef OnPlayerDisconnect
#else
    #define _ALS_OnPlayerDisconnect
#endif
#define OnPlayerDisconnect OnPlPa_OnPlayerDisconnect
forward OnPlPa_OnPlayerDisconnect(playerid,  reason);

public OnPlayerRequestClass(playerid,  classid)
{
    #if defined PRINTPAUSE
        if(InClassSelection[playerid] == false)
        {
            new NickName[MAX_PLAYER_NAME + 1];
            GetPlayerName( playerid, NickName, sizeof(NickName) );
            printf("[class] %s (id: %i) has entered class selection screen", NickName, playerid);
        }
    #endif
    InClassSelection[playerid] = true;

    if (gOnPlPa_HasCB[2])
    {
        return CallLocalFunction("OnPlPa_OnPlayerRequestClass", "ii",playerid,  classid);
    }
    return 1;
}
#if defined _ALS_OnPlayerRequestClass
    #undef OnPlayerRequestClass
#else
    #define _ALS_OnPlayerRequestClass
#endif
#define OnPlayerRequestClass OnPlPa_OnPlayerRequestClass
forward OnPlPa_OnPlayerRequestClass(playerid,  classid);

public OnPlayerSpawn(playerid)
{
    InClassSelection[playerid] = false;
    #if defined PRINTPAUSE
        new NickName[MAX_PLAYER_NAME + 1];
        GetPlayerName( playerid, NickName, sizeof(NickName) );
        printf("[spawn] %s (id: %i) has spawned", NickName, playerid);
    #endif

    if (gOnPlPa_HasCB[3])
    {
        return CallLocalFunction("OnPlPa_OnPlayerSpawn", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerSpawn
    #undef OnPlayerSpawn
#else
    #define _ALS_OnPlayerSpawn
#endif
#define OnPlayerSpawn OnPlPa_OnPlayerSpawn
forward OnPlPa_OnPlayerSpawn(playerid);

public OnPlayerUpdate(playerid)
{
    TimeOfLastUpdate[playerid] = GetTickCount();
    if(Paused[playerid] == true)
    // If the player was paused and now he sends an update, it's because he has just resumed his game.
    {
        OnPlayerUnpause(playerid);
    }

    if (gOnPlPa_HasCB[4])
    {
        return CallLocalFunction("OnPlPa_OnPlayerUpdate", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerUpdate
    #undef OnPlayerUpdate
#else
    #define _ALS_OnPlayerUpdate
#endif
#define OnPlayerUpdate OnPlPa_OnPlayerUpdate
forward OnPlPa_OnPlayerUpdate(playerid);

public OnPlayerPause(playerid)
{
    Paused[playerid] = true;
    #if defined PRINTPAUSE
        new NickName[MAX_PLAYER_NAME + 1];
        GetPlayerName( playerid, NickName, sizeof(NickName) );
        printf("[pause] %s (id: %i) has paused", NickName, playerid);
    #endif

    if (gOnPlPa_HasCB[5])
    {
        return CallLocalFunction("OnPlPa_OnPlayerPause", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerPause
    #undef OnPlayerPause
#else
    #define _ALS_OnPlayerPause
#endif
#define OnPlayerPause OnPlPa_OnPlayerPause
forward OnPlPa_OnPlayerPause(playerid);

public OnPlayerUnpause(playerid)
{
   // Delete3DTextLabel(afklabel[playerid]);
	afktime[playerid] = 0;
    Paused[playerid] = false;
    #if defined PRINTPAUSE
        new NickName[MAX_PLAYER_NAME + 1];
        GetPlayerName( playerid, NickName, sizeof(NickName) );
        printf("[resume] %s (id: %i) has returned from pausing", NickName, playerid);
    #endif

    if (gOnPlPa_HasCB[6])
    {
        return CallLocalFunction("OnPlPa_OnPlayerUnpause", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerUnpause
    #undef OnPlayerUnpause
#else
    #define _ALS_OnPlayerUnpause
Bolex_ is offline   Reply With Quote
Old 12/09/2018, 06:05 PM   #7
PoniStar
Big Clucker
 
Join Date: Aug 2018
Posts: 104
Reputation: 0
Default Re: Prevent AFK Player Damage

Bolex Not Worked , iLearner can U say what exacly i sould write under onplayertakedamge?
PoniStar is offline   Reply With Quote
Old 12/09/2018, 09:49 PM   #8
willbedie
Gangsta
 
willbedie's Avatar
 
Join Date: Feb 2016
Location: Albania
Posts: 545
Reputation: 59
Default Re: Prevent AFK Player Damage

http://forum.sa-mp.com/showthread.php?t=490436

Quote:
Originally Posted by Emmet_
IsPlayerPaused(playerid)
__________________


willbedie is offline   Reply With Quote
Old 12/09/2018, 11:33 PM   #9
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,170
Reputation: 351
Default Re: Prevent AFK Player Damage

1- There’s no need AT ALL for a whole include that big for a single function that can be done in literally 7 lines for ex:

Code:
static PlayerTick[MAX_PLAYERS];
public OnPlayerUpdate(playerid)
{
    PlayerTick[playerid] = GetTickCount();
    return 1;
}
bool:IsPlayerPaused(playerid) return ( GetTickCount() > (PlayerTick[playerid]+1500) );//Player stats didn't update for more than a second and a half.
2- Freezing the player stops any damage, freeze the player once he pauses, unfreeze once he resuems? if you find this an ordeal you can use this include i made to freeze and unfreeze when player pauses or goes afk.

Details about the include i made:

Code:
/*Functions:
__________*/
bool:IsPlayerPaused(playerid); //If the player pressed the Esc button
bool:IsPlayerAFK(playerid); //If the player isn't moving

/*Callbacks:
__________*/
public OnPlayerPause(playerid) //Once a player presses the Esc button
public OnPlayerResume(playerid) //Once a player returns from pausing

public OnPlayerAFK(playerid) //Once a player stops moving for about 55-65 seconds
public OnPlayerReturn(playerid) //Once a player starts moving again
It's an include so yeah just include it in your script using:
Code:
#include <rAfk_Checker>
LINK: CLICK HERE

For freezing players go to: http://wiki.sa-mp.com/wiki/TogglePlayerControllable

So how you would go around doing this is simply include my file and do something like this for afk players:
Code:
public OnPlayerAFK(playerid)
{
    TogglePlayerControllable(playerid, 0);
    return 1;
}

public OnPlayerReturn(playerid)
{
    TogglePlayerControllable(playerid, 1);
    return 1;
}
And change it to the pause/resume callbacks if u wanna do it when they pause as well.
__________________

[Github]:Link [Gists]:Link [Forum]:Link [Server]:Link [Discord]:Link


RogueDrifter is offline   Reply With Quote
Old 13/09/2018, 03:43 AM   #10
akib
Big Clucker
 
akib's Avatar
 
Join Date: Jun 2016
Location: Bangladesh
Posts: 175
Reputation: 1
Default Re: Prevent AFK Player Damage

First download this include : http://forum.sa-mp.com/showthread.php?t=490436

There is a function called IsPlayerPaused

OnPlayerTakeDamage:
PHP Code:
public OnPlayerTakeDamage(playeridissueridFloat:amountweaponidbodypart)
{
    new 
Float:tmp_health;
    if(
IsPlayerPaused(playerid)) //Emmet's function
    
{
        
GetPlayerHealth(playeridtmp_health);
    }else{
        
SetPlayerHealth(playeridtmp_health);
    }
    
tmp_health tmp_health-amount//Storing the health when player take damage
    
return 1;

I don't know if it works, but you can try it.
__________________
Nothing
akib is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does this prevent damage of any type? SeanDenZYR Scripting Help 1 25/05/2018 12:08 PM
[Tutorial] How to prevent damage Manyula Tutorials 8 04/05/2016 05:19 PM
prevent player from respawning Sascha Scripting Help 5 09/08/2013 12:24 PM
Prevent player from spawning. CoDeZ Scripting Help 2 21/09/2012 09:44 PM
[Help]Player damage when vehicle damage jujuv Help Archive 6 28/12/2010 01:25 AM


All times are GMT. The time now is 03:29 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.