SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 29/08/2012, 07:54 AM   #1
Glint
Guest
 
Posts: n/a
Default Checking time a player is online

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.
  Reply With Quote
Old 29/08/2012, 07:57 AM   #2
admantis
High-roller
 
Join Date: Sep 2009
Posts: 2,524
Reputation: 449
Default Respuesta: Checking time a player is online

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 Code:
#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;
}
admantis is offline   Reply With Quote
Old 29/08/2012, 08:02 AM   #3
Glint
Guest
 
Posts: n/a
Default Re: Checking time a player is online

Thank you i will try it.

EDIT: can't we just use normal variables instead of PVars ?
  Reply With Quote
Old 29/08/2012, 08:10 AM   #4
clarencecuzz
Gangsta
 
Join Date: Jul 2010
Location: Australia
Posts: 928
Reputation: 92
Default Re: Checking time a player is online

pawn Code:
#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.
clarencecuzz is offline   Reply With Quote
Old 29/08/2012, 08:12 AM   #5
Glint
Guest
 
Posts: n/a
Default Re: Checking time a player is online

Quote:
Originally Posted by clarencecuzz View Post
pawn Code:
#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.
  Reply With Quote
Old 29/08/2012, 08:18 AM   #6
iggy1
High-roller
 
iggy1's Avatar
 
Join Date: Mar 2009
Location: One past the end.
Posts: 2,421
Reputation: 273
Default Re: Checking time a player is online

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 Code:
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.
iggy1 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
Methods of checking if a radio station is online 2KY Scripting Help 0 27/02/2012 08:15 PM
Checking is player online. BaubaS Scripting Help 4 29/12/2011 02:59 PM
checking if a player is afk xDeadlyBoy Help Archive 2 09/08/2011 03:28 PM
Checking if there are 70 players online? Face9000 Help Archive 7 30/11/2010 02:07 PM
How to mark the time when a function is finished (for checking speed) ||123|| Help Archive 8 05/05/2010 10:50 AM


All times are GMT. The time now is 01:29 PM.


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