Code not working
#1

Leave saving aside it isn't showing any increment what could be wrong ?



in OnGameModeInit

pawn Код:
SetTimer("TimeUpdater",1000,true);

pawn Код:
forward TimeUpdater(playerid);
public TimeUpdater(playerid)
{
    if(IsPlayerConnected(playerid))
    {
        PlayerInfo[playerid][Seconds]++;
        if(PlayerInfo[playerid][Seconds] >= 59)
        {
            PlayerInfo[playerid][Seconds]=0;
            PlayerInfo[playerid][Minutes] = PlayerInfo[playerid][Minutes] +1;
        }
        if(PlayerInfo[playerid][Minutes] >= 59)
        {
            PlayerInfo[playerid][Minutes]=0;
            PlayerInfo[playerid][Hours] = PlayerInfo[playerid][Hours] +1;
        }
    }
    return 1;
}
When i try to test the code with command
pawn Код:
CMD:timeplayed(playerid,params[])
{
new String[200];
format(String,sizeof(String),"Time played: Seconds: %d %d Minute %d Hours",PlayerInfo[Seconds],PlayerInfo[Minutes],PlayerInfo[Hours]);
SendClientMessage(playerid,RED,String);
return 1;
}
It shows 0 0 0 only what could be wrong with it ?
Reply
#2

You can't just make up values like playerid in a function unless you pass it to it (Using SetTimerEx).

It's better to loop through all the online players:
pawn Код:
forward TimeUpdater();
public TimeUpdater()
{
    for(new i; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            PlayerInfo[i][Seconds]++;
            if(PlayerInfo[i][Seconds] >= 59)
            {
                PlayerInfo[i][Seconds]=0;
                PlayerInfo[i][Minutes] = PlayerInfo[i][Minutes] +1;
            }
            if(PlayerInfo[i][Minutes] >= 59)
            {
                PlayerInfo[i][Minutes]=0;
                PlayerInfo[i][Hours] = PlayerInfo[i][Hours] +1;
            }
        }
    }
    return 1;
}
Reply
#3

Exactly as Schneider, and I suggest using foreach for faster looping.
pawn Код:
//OnGameModeInit
    SetTimer("TimeUpdater",1000,true);
//anywhere in the script
forward TimeUpdater(playerid);
public TimeUpdater(playerid)
{
    foreach(Player, i)
    //I suggest using foreach, if you don't want you can use this
    //for(new i; i<MAX_PLAYERS; i++)
    {
        PlayerInfo[i][Seconds]++;
       
        if(PlayerInfo[i][Seconds] >= 59)
        {
            PlayerInfo[i][Seconds] = 0;
            PlayerInfo[i][Minutes] ++;
           
            if(PlayerInfo[i][Minutes] >= 59)
            {
                PlayerInfo[i][Minutes] = 0;
                PlayerInfo[i][Hours] = ++;
            }
        }
    }
    return 1;
}
Reply
#4

Still it shows 0 only.
Reply
#5

You can do it even without a timer. Just get the timestamp when player joins and whenever you need the time you get the current timestamp, subtract the joined timestamp. It will get you the amount of milliseconds the player has played, then you can convert it to hours, minutes and seconds:

pawn Код:
public OnPlayerConnect(playerid)
{
    PlayerInfo[playerid][TimeJoined] = tickcount();
    return 1;
}

CMD:timeplayed(playerid,params[])
{
    new String[32], TimeOnline, Hours, Minutes, Seconds;
    TimeOnline = (tickcount() - PlayerInfo[playerid][TimeJoined])/1000;
    Minutes = TimeOnline/60; Seconds = TimeOnline%60;
    Hours = Minutes/60; Minutes = Minutes%60;
    format(String,sizeof(String),"Time played: %d:%02d:%02d", Hours, Minutes, Seconds);
    SendClientMessage(playerid,RED,String);
    return 1;
}
Reply
#6

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

public OnGameModeInit()
{
    SetTimer("TimeUpdater",1000,true);
    return 1;
}

forward TimeUpdater(playerid);
public TimeUpdater(playerid)
{
    foreach(Player, i)
    {
        SendClientMessage(i, -1, "increased your seconds by 1");
        PlayerInfo[i][Seconds]++;

        if(PlayerInfo[i][Seconds] >= 59)
        {
            PlayerInfo[i][Seconds] = 0;
            PlayerInfo[i][Minutes] ++;

            if(PlayerInfo[i][Minutes] >= 59)
            {
                PlayerInfo[i][Minutes] = 0;
                PlayerInfo[i][Hours] = ++;
            }
        }
    }
    return 1;
}
Tell me if it says anything every second
Reply
#7

The message is being displayed "increased your seconds by 1" every second but still it shows 0 0 0 over /timeplayed :/
Reply
#8

Are you sure the player variables are not reset anywhere in the script?
Reply
#9

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

public OnGameModeInit()
{
    SetTimer("TimeUpdater",1000,true);
    return 1;
}

forward TimeUpdater(playerid);
public TimeUpdater(playerid)
{
    foreach(Player, i)
    {
        SendClientMessage(i, -1, "increased your seconds by 1");
        PlayerInfo[i][Seconds]++;

        if(PlayerInfo[i][Seconds] >= 59)
        {
            PlayerInfo[i][Seconds] = 0;
            PlayerInfo[i][Minutes] ++;

            if(PlayerInfo[i][Minutes] >= 59)
            {
                PlayerInfo[i][Minutes] = 0;
                PlayerInfo[i][Hours] = ++;
            }
        }
    }
    return 1;
}

CMD:timeplayed(playerid,params[])
{
    new String[128];
    format(String, sizeof(String), "Time played: Seconds: %d %d Minute %d Hours", PlayerInfo[playerid][Seconds],PlayerInfo[playerid][Minutes],PlayerInfo[playerid][Hours]);
    SendClientMessage(playerid,RED,String);
    return 1;
}
How are you declaring your enum, it seems you're not adding [playerid] to them, try this code and if it didn't work show me your enum for players info
Reply
#10

Edit:

Did some edits and with both of your help its working now thanks rep for both of u
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)