SA-MP Forums Archive
Checking time a player is online - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Checking time a player is online (/showthread.php?tid=373068)



Checking time a player is online - Glint - 29.08.2012

Hello guys i am new to this time thing i just want to know how do you make something like this.

let's say a player has joined the server and the server is restricted to 2 hours (meaning anyone when joining can play 2 hours) < for example.

then after the 2 hours it will kick him.

Thanks in advance a general concept of how it is done is good too.


Respuesta: Checking time a player is online - admantis - 29.08.2012

make a timer without parameters with an interval of 1 second and run it, and then increase a value that will count as the seconds. this way you can determine how many seconds each player has been online during their session.

pawn Код:
#include a_samp
public OnGameModeInit()
{
    SetTimer("CountPlayersTime", 1000, true);
    return;
}

forward CountPlayersTime();
public CountPlayersTime()
{
    foreach(Player, i)
    {
        SetPVarInt(i, "OnlineSeconds", GetPVarInt(playerid, "OnlineSeconds"));
        if(GetPVarInt(i, "OnlineSeconds") == 3600*2) // 3600 secs = 1 hour
        {
            Kick(i);
        }
    }
    return;
}
CMD:myonlinetime(playerid,params[])
{
    new output[32];
    format(output, sizeof (output), "%d seconds online", GetPVarInt(playerid, "OnlineSeconds"));
    SendClientMessage(playerid, -1, output);
    return 1;
}



Re: Checking time a player is online - Glint - 29.08.2012

Thank you i will try it.

EDIT: can't we just use normal variables instead of PVars ?


Re: Checking time a player is online - clarencecuzz - 29.08.2012

pawn Код:
#include <a_samp>
#include <foreach>

new Time[MAX_PLAYERS];
public OnFilterScriptInit() //Or OnGameModeInit() if you're using a Game Mode.
{
    SetTimer("PlayerTime", 1000, true);
    return 1;
}

public OnPlayerConnect(playerid)
{
    Time[playerid] = 0;
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    Time[playerid] = 0;
    return 1;
}

forward PlayerTime();
public PlayerTime()
{
    /*for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        { //If you're not using the 'foreach' include.*/

    foreach(Player, i)
    {
        if(Time[i] < 7200)
        {
            Time[i]++;
        }
        else
        {
            SendClientMessage(i, 0xFFFF00FF, "You Have Reached 2 Hours Of Playing Time. Therefore You Have Been Kicked For Reaching The Limit.");
            Kick(i);
        }
    }
    return 1;
}
You need to explain why they're getting kicked. You will need the foreach include for this script, otherwise you can use the commented lines as an alternative.


Re: Checking time a player is online - Glint - 29.08.2012

Quote:
Originally Posted by clarencecuzz
Посмотреть сообщение
pawn Код:
#include <a_samp>
#include <foreach>

new Time[MAX_PLAYERS];
public OnFilterScriptInit() //Or OnGameModeInit() if you're using a Game Mode.
{
    SetTimer("PlayerTime", 1000, true);
    return 1;
}

public OnPlayerConnect(playerid)
{
    Time[playerid] = 0;
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    Time[playerid] = 0;
    return 1;
}

forward PlayerTime();
public PlayerTime()
{
    /*for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        { //If you're not using the 'foreach' include.*/

    foreach(Player, i)
    {
        if(Time[i] < 7200)
        {
            Time[i]++;
        }
        else
        {
            SendClientMessage(i, 0xFFFF00FF, "You Have Reached 2 Hours Of Playing Time. Therefore You Have Been Kicked For Reaching The Limit.");
            Kick(i);
        }
    }
    return 1;
}
You need to explain why they're getting kicked. You will need the foreach include for this script, otherwise you can use the commented lines as an alternative.
Thanks for the response.


Re: Checking time a player is online - iggy1 - 29.08.2012

If your not too bothered about being exact with your timing, here's a version that only checks every 5 minutes (so it can be up to 5 mins out). You can make it more accurate by reducing the 'time' arg on the timer.

pawn Код:
new gPlayerJoinedAt[MAX_PLAYERS];

forward TimePlayedCheck();

public OnGameModeInit()
{
    SetTimer("TimePlayedCheck", 5*60000, true);//reduce time for more accuracy.
    return 1;
}

public OnPlayerConnect(playerid)
{
    //store time player joined
    gPlayerJoinedAt[playerid] = gettime();
    return 1;
}

public TimePlayedCheck()
{
    foreach(new i : Player)
    {
        //check if 7200 seconds(2hrs) have passed since player connected
        if((gettime()-gPlayerJoinedAt[i])>=7200)//subtract the time the player joined from the current time
        {
            kick(i);
        }
    }
}
Also worth noting: admins will be kicked indiscriminately with the above code.