SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 26/05/2020, 10:17 AM   #1
mems
Big Clucker
 
Join Date: Oct 2019
Posts: 142
Reputation: 0
Default Data being reset when connecting to server.

hello,

i've been encountering issues lately as i was trying to fix some commands, and i noticed that everytime i left the server and joined, data (admin level, score, money) was being reset everytime. i checked the functions, onplayerconnect, onplayerdisconnect, the load data function, and it looked normal to me, i even tried changing my login code a bit, but same outcome, reset every time. here's the code, if you need more information, please tell me.

OnPlayerConnect:

pawn Code:
public OnPlayerConnect(playerid)
{
//================| Variables reset so as to avoid corruption |===============//
    PlayerInfo[playerid][IP] = 0;
    PlayerInfo[playerid][AdminLevel] = 0;
    PlayerInfo[playerid][Money] = 0;
    PlayerInfo[playerid][Score] = 0;
    PlayerInfo[playerid][Kills] = 0;
    PlayerInfo[playerid][Deaths] = 0;
    PlayerInfo[playerid][LoginAttempts] = 0;
    dm[playerid] = 0;
    Streak[playerid] = 0;
    Hitsound[playerid] = 0;
//============================| Register/Login |==============================//
    new pname[MAX_PLAYER_NAME], string[192], admrank[64];
    GetPlayerName(playerid, pname, sizeof(pname));
    TogglePlayerSpectating(playerid, 1);
    if(fexist(UserPath(playerid)))
    {
        INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);
        format(string, sizeof(string), "{FFFFFF}Welcome back, the account \"{FF0000}%s{FFFFFF}\" is registered.\nIn order to play, you must login by typing your password below.", pname);
        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "{FF0000}Login", string, "Okay", "Quit");
        if(IsPlayerAdmin(playerid))
        {
            admrank = "{FFA500}[{FF0000}RCON{FFA500}]";
            format(string, sizeof(string), "{696969}[{00FF00}+{696969}]{00FF00} %s {FFFFFF}%s (%d) {00FF00}has joined the server!", admrank, pname, playerid);
        }
        else
        {
            if(PlayerInfo[playerid][AdminLevel] > 0)
            {
                switch(PlayerInfo[playerid][AdminLevel])
                {
                    case 1: admrank = "{FFA500}[{FFFF00}Moderator{FFA500}]";
                    case 2: admrank = "{FFA500}[{FF0000}Admin{FFA500}]";
                    case 3: admrank = "{FFA500}[{3366FF}Manager{FFA500}]";
                }
                format(string, sizeof(string), "{696969}[{00FF00}+{696969}]{00FF00} %s {FFFFFF}%s (%d) {00FF00}has joined the server!", admrank, pname, playerid);
            }
            else
            {
                format(string, sizeof(string), "{696969}[{00FF00}+{696969}]{FFFFFF} %s (%d) {00FF00}has joined the server!", pname, playerid);
            }
        }
        SendClientMessageToAll(COLOR_LIME, string);
    }
    else
    {
        format(string, sizeof(string), "{FFFFFF}Welcome, the account \"{FF0000}%s{FFFFFF}\" is not registered.\nIn order to play, you must register by typing a password below.", pname);
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "{FF0000}Registration", string, "Okay", "Quit");
        format(string, sizeof(string), "{696969}[{00FF00}+{696969}]{00FF00} \"{FFFFFF}%s{00FF00}\" (%d) has joined the server for the first time!", pname, playerid);
    }
    if(PlayerInfo[playerid][Banned] == 1)
    {
        format(string, sizeof(string), "{696969}[{FFA500}SECURITY{696969}]: {FF0000}%s {FFFFFF}tried to join, but is banned! {696969}({FFA500}Permanent{696969})", pname);
        SendToStaff(COLOR_WHITE, string);
        printf(string);
        SetTimerEx("DelayKick",100,0,"i",playerid);
    }
    return 1;
}

OnPlayerDisconnect:

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    new string[192], pname[MAX_PLAYER_NAME], admrank[64];
    GetPlayerName(playerid,pname,MAX_PLAYER_NAME);
//==============================| Messages |==================================//
    if(IsPlayerAdmin(playerid))
    {
        admrank = "{FFA500}[{FF0000}RCON{FFA500}]";
        switch(reason)
        {
            case 0: format(string,sizeof(string),"{696969}[{FF0000}-{696969}] %s{FFFFFF} %s (%d) {FF0000}has left the server. (Timeout/Crash)",admrank, pname, playerid);
            case 1: format(string,sizeof(string),"{696969}[{FF0000}-{696969}] %s{FFFFFF} %s (%d) {FF0000}has left the server. (Leaving)",admrank, pname, playerid);
            case 2: format(string,sizeof(string),"{696969}[{FF0000}-{696969}] %s{FFFFFF} %s (%d) {FF0000}has left the server. (Kicked/Banned)",admrank, pname, playerid);
        }
    }
    else
    {
        if(PlayerInfo[playerid][AdminLevel] > 0)
        {
            switch(PlayerInfo[playerid][AdminLevel])
            {
                case 1: admrank = "{FFA500}[{FFFF00}Moderator{FFA500}]";
                case 2: admrank = "{FFA500}[{FF0000}Admin{FFA500}]";
                case 3: admrank = "{FFA500}[{3366FF}Manager{FFA500}]";
            }
            switch(reason)
            {
                case 0: format(string,sizeof(string),"{696969}[{FF0000}-{696969}] %s{FFFFFF} %s (%d) {FF0000}has left the server. (Timeout/Crash)",admrank, pname, playerid);
                case 1: format(string,sizeof(string),"{696969}[{FF0000}-{696969}] %s{FFFFFF} %s (%d) {FF0000}has left the server. (Leaving)",admrank, pname, playerid);
                case 2: format(string,sizeof(string),"{696969}[{FF0000}-{696969}] %s{FFFFFF} %s (%d) {FF0000}has left the server. (Kicked/Banned)",admrank, pname, playerid);
            }
        }
        else
        {
            switch(reason)
            {
                case 0: format(string,sizeof(string),"{696969}[{FF0000}-{696969}]{FFFFFF} %s (%d) {FF0000}has left the server. (Timeout/Crash)",pname, playerid);
                case 1: format(string,sizeof(string),"{696969}[{FF0000}-{696969}]{FFFFFF} %s (%d) {FF0000}has left the server. (Leaving)",pname, playerid);
                case 2: format(string,sizeof(string),"{696969}[{FF0000}-{696969}]{FFFFFF} %s (%d) {FF0000}has left the server. (Kicked/Banned)",pname, playerid);
            }
        }
    }
    SendClientMessageToAll(COLOR_LIME, string);
    printf(string);
//============================| Saving Variables |============================//
    SavePlayerStats(playerid);
    dm[playerid] = 0;
    Streak[playerid] = 0;
    Hitsound[playerid] = 0;
    for(new i; i < 6; i++)
    {
        DeletePlayer3DTextLabel(playerid, Info[playerid]);
    }
    return 1;
}

Register + Login system (Y_INI)

pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case DIALOG_REGISTER:
        {
            if (!response) return Kick(playerid);
            if(response)
            {
                new string[192], pip[16], pname[MAX_PLAYER_NAME];
                GetPlayerIp(playerid, pip, sizeof(pip));
                GetPlayerName(playerid, pname, sizeof(pname));
                if(!strlen(inputtext))
                {
                    SendClientMessage(playerid, COLOR_WHITE, "[{FF0000}REGISTER{FFFFFF}]:{FF0000} You have to enter your desired password.");
                    format(string, sizeof(string), "{FFFFFF}Welcome, the account \"{FF0000}%s{FFFFFF}\" is not registered.\nIn order to play, you must register by typing a password below.", pname);
                    return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "{FFFFFF}Registration", string, "Register", "Quit");
                }
                new INI:file = INI_Open(UserPath(playerid));
                INI_SetTag(file, "=| Player Data |=");
                INI_WriteInt(file, "Password", udb_hash(inputtext));
                INI_WriteString(file,"IP", pip);
                INI_WriteInt(file, "Cash", 0);
                INI_WriteInt(file, "Score", 0);
                INI_WriteInt(file, "Kills", 0);
                INI_WriteInt(file, "Deaths", 0);
                INI_WriteInt(file, "AdminLevel", 0);
                INI_WriteString(file, "Registered", "Yes");
                INI_Close(file);
                SendClientMessage(playerid, COLOR_WHITE, "[{FF0000}REGISTER{FFFFFF}]:{00FF00} You have now successfully registered, enjoy your stay!");
                PlayerPlaySound(playerid,1057,0.0,0.0,0.0);
                PlayerInfo[playerid][Registered] = true;
                TogglePlayerSpectating(playerid, 0);
                GivePlayerMoney(playerid, 50000);
            }
        }
        case DIALOG_LOGIN:
        {
            if (!response)
            {
                Kick(playerid);
                return 1;
            }
            else
            {
                new string[192], pname[MAX_PLAYER_NAME], admrank[32];
                GetPlayerName(playerid, pname, sizeof(pname));
                if(udb_hash(inputtext) == PlayerInfo[playerid][Password])
                {
                    PlayerInfo[playerid][LoginAttempts]++;
                    if(PlayerInfo[playerid][LoginAttempts] >= MAX_LOGIN_ATTEMPTS)
                    {
                        format(string, sizeof(string), "[{FF0000}LOGIN{FFFFFF}]: %s (%d) has been kicked from the server for exceeding login attempts.", pname, playerid);
                        SendClientMessageToAll(COLOR_WHITE, string);
                        SetTimerEx("DelayKick",100,0,"i",playerid);
                    }
                    else
                    {
                        format(string, sizeof(string), "{FFFFFF}You have entered an incorrect password. {FF0000}(Attempts: %i)", PlayerInfo[playerid][LoginAttempts]);
                        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{FFFFFF}Login", string, "Login", "Quit");
                    }
                }
                else
                {
                    INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);
                    new INI:file = INI_Open(UserPath(playerid));
                    INI_SetTag(file, "=| Player Data |=");
                    INI_WriteString(file, "Registered", "Yes");
                    INI_Close(file);
                    SetPlayerScore(playerid, PlayerInfo[playerid][Score]);
                    GivePlayerMoney(playerid, PlayerInfo[playerid][Money]);
                    PlayerPlaySound(playerid,1057,0.0,0.0,0.0);
                    PlayerInfo[playerid][Registered] = true;
                    TogglePlayerSpectating(playerid, false);
                    if(PlayerInfo[playerid][AdminLevel] > 0)
                    {
                        if(IsPlayerAdmin(playerid))
                        {
                            admrank = "{FF0000}RCON";
                        }
                        else
                        {
                            switch(PlayerInfo[playerid][AdminLevel])
                            {
                                case 1: admrank = "{FFFF00}Moderator";
                                case 2: admrank = "{FF0000}Admin";
                                case 3: admrank = "{3366FF}Manager";
                            }
                        }
                        format(string, sizeof(string), "[{FF0000}LOGIN{FFFFFF}]:{00FF00} Welcome %s, you have been successfully logged in! (Level: %d | Rank: %s{00FF00})", pname, PlayerInfo[playerid][AdminLevel], admrank);
                        SendClientMessage(playerid, COLOR_WHITE, string);
                    }
                    else
                    {
                        format(string, sizeof(string), "[{FF0000}LOGIN{FFFFFF}]:{00FF00} Welcome %s, you have been successfully logged in!", pname);
                        SendClientMessage(playerid, COLOR_WHITE, string);
                    }
                }
            }
        }

The loading function:

pawn Code:
forward LoadUser_data(playerid,name[],value[]);
public LoadUser_data(playerid,name[],value[])
{
    INI_Int("Password",PlayerInfo[playerid][Password]);
    INI_Int("Cash",PlayerInfo[playerid][Money]);
    INI_Int("AdminLevel",PlayerInfo[playerid][AdminLevel]);
    INI_Int("Score",PlayerInfo[playerid][Score]);
    INI_Int("Kills",PlayerInfo[playerid][Kills]);
    INI_Int("Deaths",PlayerInfo[playerid][Deaths]);
    INI_Int("Banned",PlayerInfo[playerid][Banned]);
    INI_String("IP", PlayerInfo[playerid][IP], 16);
    return 1;
}

The saving function:

pawn Code:
SavePlayerStats(playerid)
{
    if(PlayerInfo[playerid][Registered] == true)
    {
        new INI:file = INI_Open(UserPath(playerid));
        PlayerInfo[playerid][Money] = GetPlayerMoney(playerid);
        PlayerInfo[playerid][Score] = GetPlayerScore(playerid);
        INI_SetTag(file,"=| Player Data |=");
        INI_WriteInt(file,"AdminLevel",PlayerInfo[playerid][AdminLevel]);
        INI_WriteInt(file,"Cash",PlayerInfo[playerid][Money]);
        INI_WriteInt(file,"Score",PlayerInfo[playerid][Score]);
        INI_WriteInt(file,"Kills",PlayerInfo[playerid][Kills]);
        INI_WriteInt(file,"Deaths",PlayerInfo[playerid][Deaths]);
        INI_WriteInt(file,"Banned",PlayerInfo[playerid][Banned]);
        INI_WriteString(file,"Registered",PlayerInfo[playerid][Registered]);
        INI_Close(file);
    }
}

i don't know what else to send, as that's the only thing i have as evidence or something.
mems is offline   Reply With Quote
Old 26/05/2020, 12:53 PM   #2
mems
Big Clucker
 
Join Date: Oct 2019
Posts: 142
Reputation: 0
Default Re: Data being reset when connecting to server.

bump..
mems is offline   Reply With Quote
Old 26/05/2020, 03:42 PM   #3
Grim_
High-roller
 
Grim_'s Avatar
 
Join Date: Sep 2008
Location: United States
Posts: 2,897
Reputation: 118
Default Re: Data being reset when connecting to server.

Is it actually being saved to the file correctly? If so, debug the file loading function to see what values are being read to see if your issue is there.
Grim_ is offline   Reply With Quote
Old 26/05/2020, 04:30 PM   #4
mems
Big Clucker
 
Join Date: Oct 2019
Posts: 142
Reputation: 0
Default Re: Data being reset when connecting to server.

Quote:
Originally Posted by Grim_ View Post
Is it actually being saved to the file correctly? If so, debug the file loading function to see what values are being read to see if your issue is there.
not sure what i should do, but should I put "printf" on OnPlayerDisconnect and then add the enums which show values ?

EDIT: I did this:
pawn Code:
format(string, sizeof(string), "Admin Level: %d, Money: %d, Score: %d", PlayerInfo[playerid][AdminLevel], PlayerInfo[playerid][Money], PlayerInfo[playerid][Score]);
then added "printf(string);" It displayed normally what I was expecting it to. but when i joined the server, everything got reset..
mems is offline   Reply With Quote
Old 26/05/2020, 06:17 PM   #5
mems
Big Clucker
 
Join Date: Oct 2019
Posts: 142
Reputation: 0
Default Re: Data being reset when connecting to server.

bump...
mems is offline   Reply With Quote
Old 26/05/2020, 07:51 PM   #6
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 872
Reputation: 69
Default Re: Data being reset when connecting to server.

I don't understand, why are you resetting data when a player connect to reload them after login?

I suggest you to move the reset code you have in OnPlayerConnect and place it BEFORE the LoadUserData in the login data. Here to be precise:

pawn Code:
INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);

Place this code:

pawn Code:
PlayerInfo[playerid][IP] = 0;
    PlayerInfo[playerid][AdminLevel] = 0;
    PlayerInfo[playerid][Money] = 0;
    PlayerInfo[playerid][Score] = 0;
    PlayerInfo[playerid][Kills] = 0;
    PlayerInfo[playerid][Deaths] = 0;
    PlayerInfo[playerid][LoginAttempts] = 0;

Above the one i posted and remove it from OnPlayerConnect.
Symon is offline   Reply With Quote
Old 26/05/2020, 08:05 PM   #7
mems
Big Clucker
 
Join Date: Oct 2019
Posts: 142
Reputation: 0
Default Re: Data being reset when connecting to server.

Quote:
Originally Posted by Symon View Post
I don't understand, why are you resetting data when a player connect to reload them after login?

I suggest you to move the reset code you have in OnPlayerConnect and place it BEFORE the LoadUserData in the login data. Here to be precise:

pawn Code:
INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);

Place this code:

pawn Code:
PlayerInfo[playerid][IP] = 0;
    PlayerInfo[playerid][AdminLevel] = 0;
    PlayerInfo[playerid][Money] = 0;
    PlayerInfo[playerid][Score] = 0;
    PlayerInfo[playerid][Kills] = 0;
    PlayerInfo[playerid][Deaths] = 0;
    PlayerInfo[playerid][LoginAttempts] = 0;

Above the one i posted and remove it from OnPlayerConnect.
the thing is that i've never touched those codes until that YSI update i had to do, then it somehow won't work, from my point of view everything looks okay and correct, and also the data reset is used so as not to cause corruption

EDIT: i got it from that person: https://forum.sa-mp.com/showthread.php?t=597639
mems is offline   Reply With Quote
Old 26/05/2020, 10:50 PM   #8
mems
Big Clucker
 
Join Date: Oct 2019
Posts: 142
Reputation: 0
Default Re: Data being reset when connecting to server.

bump...
mems is offline   Reply With Quote
Old 26/05/2020, 11:06 PM   #9
xRadical3
Gangsta
 
xRadical3's Avatar
 
Join Date: May 2017
Location: Iran
Posts: 546
Reputation: 20
Default Re: Data being reset when connecting to server.

This post will not help your problem, but I will recommend it to you;
If you are building a new game mode, it is better to use mysql
files to save player data is slow and outdated.
https://forum.sa-mp.com/showthread.php?t=627520
xRadical3 is offline   Reply With Quote
Old 27/05/2020, 12:17 AM   #10
mems
Big Clucker
 
Join Date: Oct 2019
Posts: 142
Reputation: 0
Default Re: Data being reset when connecting to server.

Quote:
Originally Posted by Vizi10 View Post
This post will not help your problem, but I will recommend it to you;
If you are building a new game mode, it is better to use mysql
files to save player data is slow and outdated.
https://forum.sa-mp.com/showthread.php?t=627520
MYSQL is a million times harder than y ini, and no, still won't prefer mysql. any other ideas on how to fix this thing?
i literally cannot save any data...
mems 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
Reset Data SoFahim Scripting Help 5 29/08/2015 01:40 PM
Reset data (pickup, dynamic area) qmnty Scripting Help 0 12/01/2015 02:13 AM
Reset Weapon Data hoangtn219 Scripting Help 6 12/12/2014 01:49 PM
Reset all mysql data when I write /rcon gmx Dystans Scripting Help 2 14/04/2013 05:44 PM
Reset array data Killer#Mummy Scripting Help 2 14/08/2012 04:02 PM


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


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