Search

open.mp forum

RegisterLogin

Discussion

General
Chat
Tech
Life
Art
Programming
open.mp
Development Updates
Questions and Suggestions
SA-MP
General Discussions
Videos and Screenshots
Support
Pawn Scripting
Tutorials
Releases
Plugins
Libraries
Filterscripts
Gamemodes
Maps
Advertisements
Other languages
Spanish/Espa?ol
Programaci?n
Discusi?n GTA SA Multijugador
Mods
Offtopic
Juegos
Portuguese/Portugu?s
Russian/???????
Italian/Italiano
Dutch/Nederlands
German/Deutsch
Romanian/Rom?na
Ex-Yu
Polish/Polski
Og?lne
Serwery
Skryptowanie
Filmiki i zdjecia
Lithuanian/Lietuvi?kas
French/Fran?ais
Hungarian/Magyar
Hindi/Urdu
Turkish
Other
Internal
Team
Hidden
Archived

Library

 Collections Links Members Roles

How to save the position/skin of the player after exiting?

Hi, im currently scripting a server and i want to know how a players progress is saved on the server??

ID
d6ouivnilegovhg7bpq0
author
crypticsin's avatar

CrypticSin

@crypticsin


View profile
Copy link
  Report member
started
Apr 3, 2021
replies
10
participating
No

scroll to top

powered by storyden

Login
Discussion
Pawn Scripting
How to save the position/skin of the player after exiting?
crypticsin's avatar

CrypticSin

@crypticsin


View profile
Copy link
  Report member
• 5y
Pawn Scripting

How to save the position/skin of the player after exiting?

pawn

Hi, im currently scripting a server and i want to know how a players progress is saved on the server??



So for example, when the user selects their skin i want the player to keep their skin until they decide to change it manually,?instead of the skin selection appearing everytime someone logs in.



Also how do you save the position of a player when they exit the game? and when they log back?back in it should log the user back to the same posiiton they exited?



What are the areas in the SAMP-wiki should i look into to make something like this?

0 likes10 replies
  1. pinch's avatar

    Pinch

    @pinch


    View profile
    Copy link
      Report member
    • 5y

    Databases;

    Learn SQL syntax than learn MySQL!

  2. robertocaribbean's avatar

    robertocaribbean

    @robertocaribbean


    View profile
    Copy link
      Report member
    • 5y

    I recommend you to start by doing something simple like store money, score.. then when you come close to understand how to store basic things, you can move on into more advanced topics.

    A great tutorial for starters: https://sampforumarchive.com/forum.sa-mp.com/showthreadba7c.html?t=449536

    Also, I recommend you to see other people his code if it's possible and try to understand the process that the programmer follows in his code.

    In the bottom of this page https://open.mp/docs/awesome there are many gamemodes that you can view in github.

  3. destiezk's avatar

    destiezk

    @destiezk


    View profile
    Copy link
      Report member
    • 5y

    Honestly, if you have lack of experience with SQL you can try using Dini for it, although it's not the best.



    QuoteSo for example, when the user selects their skin i want the player to keep their skin until they decide to change it manually, instead of the skin selection appearing everytime someone logs in.





    If you select a skin, it will be the same after each respawn, if I correctly know.?

    You can make a /skin command, store the skin id in a global array for MAX_PLAYERS, then call it with SetPlayerSkin at OnPlayerSpawn.

  4. destiezk's avatar

    destiezk

    @destiezk


    View profile
    Copy link
      Report member
    • 5y

    Honestly, if you have lack of experience with SQL you can try using Dini for it, although it's not the best.



    QuoteSo for example, when the user selects their skin i want the player to keep their skin until they decide to change it manually, instead of the skin selection appearing everytime someone logs in.





    If you select a skin, it will be the same after each respawn, if I correctly know.?

    You can make a /skin command, store the skin id in a global array for MAX_PLAYERS, then call it with SetPlayerSkin at OnPlayerSpawn.

  5. crypticsin's avatar

    CrypticSin

    @crypticsin


    View profile
    Copy link
      Report member
    • 5y

    robertocaribbean said:

    I recommend you to start by doing something simple like store money, score.. then when you come close to understand how to store basic things, you can move on into more advanced topics.



    A great tutorial for starters: https://sampforumarchive.com/forum.sa-mp.com/showthreadba7c.html?t=449536



    Also, I recommend you to see other people his code if it's possible and try to understand the process that the programmer follows in his code.



    In the bottom of this page https://open.mp/docs/awesome there are many gamemodes that you can view in github.





    I know how to store user information, like the username, password, register date and all that but i was wondering if this has a similiar approach with saving skins and position of a player?



    Im using phpmyadmin for mysql, and most people use Y_INI in their gamemodes. If it get's really complicated i might have to switch to Y_INI to store user data unless i find a way to do it on MySql.

  6. robertocaribbean's avatar

    robertocaribbean

    @robertocaribbean


    View profile
    Copy link
      Report member
    • 5y

    CrypticSin said:

    I know how to store user information, like the username, password, register date and all that but i was wondering if this has a similiar approach with saving skins and position of a player?



    Im using phpmyadmin for mysql, and most people use Y_INI in their gamemodes. If it get's really complicated i might have to switch to Y_INI to store user data unless i find a way to do it on MySql.



    I will show you an example of storing a player's skin.

    I will use SQLite, but for MySQL, it's the same approach with different function names when you associate the result of the fields on a variable. Anyway, I have no experience with MySQL, so take these words carefully.

    #include <a_samp>
    
    // Suppose USER_ID and USER_NICKNAME are already associated with the database, that is to say, you already have a login/register system, so I only show you how to store skin information when the player exits and how to set the player's skin when the player connects.
    
    new DB:db_handle; // Create a global variable to make a db connection and manipulate later on the code.
    
    enum USER_DATA {
    ? ? USER_ID,
    ? ? USER_NICKNAME,
    ? ? USER_SKIN // We need to use this only for when the player connects.
    };
    
    new gUserData;
    
    public OnGameModeInit() {
    ? ? db_handle = db_open("main.db");
    
    ? ? if (db_handle) {
    ? ? ? ? print("Successfully created a connection to database \"main.db\".");
    
    ? ? ? ? db_free_result(db_query(db_handle, "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, nickname TEXT, skin INTEGER)"));
    ? ? }
    
    ? ? else {
    ? ? ? ? print("Failed to open a connection to database \"main.db\".");
    ? ? }
    ? ? return 1;
    }
    
    // To store the skin that the player has before exit, all you need to do is to get the player skin and use the UPDATE clause from SQL.
    public OnPlayerDisconnect(playerid, reason) {
    ? ? new query;
    
    ? ? format(query, sizeof query, "UPDATE users SET skin = '%i' WHERE nickname ='%q'", GetPlayerSkin(playerid), gUserData);
    
    ? ? db_query(db_handle, query);
    
    ? ? // Reset player variables
    ? ? new 
    ? ? ? ? tmp;
    
    ? ? gUserData = tmp; 
    ? ? return 1;
    }
    
    // Now you need to set the skin stored in the database to the player, and then make it spawn with that skin for example.
    public OnPlayerConnect(playerid)
    {
    // Reset player variables
        new 
            tmp;
    gUserData = tmp; 
    
    ? ? new query, DBResult: result; // Create a query variable and DBResult to free the result of the query.
    
    ? ? // Suppose we have a function to check the player login succefully.
    ? ? if (login()) { 
    ? ? ? ? SendClientMessage(playerid, -1, "Login succefully.");
    
    ? ? ? ? // We make the query, selecting all fields from the users table.
    ? ? ? ? format(query, sizeof query, "SELECT * FROM users WHERE nickname='%q' LIMIT 1", gUserData);
    
    ? ? ? ? result = db_query(db_handle, query);
    
    ? ? ? ? // If there any row, it says that the user registered has the fields that you declare before (id, nickname and skin).
    ? ? ? ? if (db_num_rows (result)) {
    ? ? ? ? ? ? // So we will proceed to associate the result of the field "skin" from the database to our variable gUserData.
    ? ? ? ? ? ? gUserData = db_get_field_assoc_int(result, "skin");
    ? ? ? ? }
    ? ? }
    
    else {
    ? ? ? ? ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "La contrase?a que ingresaste es incorrecta.\nPor favor, intenta de nuevo!", "Ingresar", "Salir");
    ? ? }
    
    ? ? db_free_result(result); // Free the query result.
    return 1;
    }
    
    public OnPlayerSpawn(playerid) {
    ? ? SetSpawnInfo(playerid, 0, gUserData, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // Here is where we use the value already passed to our variable from the database.
    ? ? SpawnPlayer(playerid);
    ? ? return 1;
    }



    I hope this helps you a little bit!

  7. crypticsin's avatar

    CrypticSin

    @crypticsin


    View profile
    Copy link
      Report member
    • 5y

    robertocaribbean said:

    CrypticSin said:



    I know how to store user information, like the username, password, register date and all that but i was wondering if this has a similiar approach with saving skins and position of a player?







    Im using phpmyadmin for mysql, and most people use Y_INI in their gamemodes. If it get's really complicated i might have to switch to Y_INI to store user data unless i find a way to do it on MySql.







    I will show you an example of storing a player's skin.



    I will use SQLite, but for MySQL, it's the same approach with different function names when you associate the result of the fields on a variable. Anyway, I have no experience with MySQL, so take these words carefully.



    #include <a_samp>
    
    
    
    // Suppose USER_ID and USER_NICKNAME are already associated with the database, that is to say, you already have a login/register system, so I only show you how to store skin information when the player exits and how to set the player's skin when the player connects.
    
    
    
    new DB:db_handle; // Create a global variable to make a db connection and manipulate later on the code.
    
    
    
    enum USER_DATA {
    
    ? ? USER_ID,
    
    ? ? USER_NICKNAME,
    
    ? ? USER_SKIN // We need to use this only for when the player connects.
    
    };
    
    
    
    new gUserData;
    
    
    
    public OnGameModeInit() {
    
    ? ? db_handle = db_open("main.db");
    
    
    
    ? ? if (db_handle) {
    
    ? ? ? ? print("Successfully created a connection to database \"main.db\".");
    
    
    
    ? ? ? ? db_free_result(db_query(db_handle, "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, nickname TEXT, skin INTEGER)"));
    
    ? ? }
    
    
    
    ? ? else {
    
    ? ? ? ? print("Failed to open a connection to database \"main.db\".");
    
    ? ? }
    
    ? ? return 1;
    
    }
    
    
    
    // To store the skin that the player has before exit, all you need to do is to get the player skin and use the UPDATE clause from SQL.
    
    public OnPlayerDisconnect(playerid, reason) {
    
    ? ? new query;
    
    
    
    ? ? format(query, sizeof query, "UPDATE users SET skin = '%i' WHERE nickname ='%q'", GetPlayerSkin(playerid), gUserData);
    
    
    
    ? ? db_query(db_handle, query);
    
    
    
    ? ? // Reset player variables
    
    ? ? new 
    
    ? ? ? ? tmp;
    
    
    
    ? ? gUserData = tmp; 
    
    ? ? return 1;
    
    }
    
    
    
    // Now you need to set the skin stored in the database to the player, and then make it spawn with that skin for example.
    
    public OnPlayerConnect(playerid)
    
    {
    
    ? ? new query, DBResult: result; // Create a query variable and DBResult to free the result of the query.
    
    
    
    ? ? // Suppose we have a function to check the player login succefully.
    
    ? ? if (login()) { 
    
    ? ? ? ? SendClientMessage(playerid, -1, "Login succefully.");
    
    
    
    ? ? ? ? // We make the query, selecting all fields from the users table.
    
    ? ? ? ? format(query, sizeof query, "SELECT * FROM users WHERE nickname='%q' LIMIT 1", gUserData);
    
    
    
    ? ? ? ? result = db_query(db_handle, query);
    
    
    
    ? ? ? ? // If there any row, it says that the user registered has the fields that you declare before (id, nickname and skin).
    
    ? ? ? ? if (db_num_rows (result)) {
    
    ? ? ? ? ? ? // So we will proceed to associate the result of the field "skin" from the database to our variable gUserData.
    
    ? ? ? ? ? ? gUserData = db_get_field_assoc_int(result, "skin");
    
    ? ? ? ? }
    
    ? ? }
    
    
    
    else {
    
    ? ? ? ? ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "La contrase?a que ingresaste es incorrecta.\nPor favor, intenta de nuevo!", "Ingresar", "Salir");
    
    ? ? }
    
    
    
    ? ? db_free_result(result); // Free the query result.
    
    return 1;
    
    }
    
    
    
    public OnPlayerSpawn(playerid) {
    
    ? ? SetSpawnInfo(playerid, 0, gUserData, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // Here is where we use the value already passed to our variable from the database.
    
    ? ? SpawnPlayer(playerid);
    
    ? ? return 1;
    
    }





    I hope this helps you a little bit!





    Hi thanks for your help!?

    What if i wanted to store more user data like player kills, gamescore, deaths etc.. Will i have to follow the same pattern as you showed for the skins or is there a shorter way of saving these information?

  8. robertocaribbean's avatar

    robertocaribbean

    @robertocaribbean


    View profile
    Copy link
      Report member
    • 5y

    CrypticSin said:

    Hi thanks for your help!?

    What if i wanted to store more user data like player kills, gamescore, deaths etc.. Will i have to follow the same pattern as you showed for the skins or is there a shorter way of saving these information?



    For the game score, it's simple because there are built-in functions that keep track of the score of a player, like "GetPlayerScore", so you need to get the player's score before he exits, use UPDATE, and then when he connects use "SetPlayerScore" in a similar way as we have to do it with their skin.

    For other things like player kills and deaths, I guess you have to make a custom function that keep track of that values and then do the same process that you need to do for the score or skin.

  9. robertocaribbean's avatar

    robertocaribbean

    @robertocaribbean


    View profile
    Copy link
      Report member
    • 5y

    EDIT: Double post by error, sorry.

  10. crypticsin's avatar

    CrypticSin

    @crypticsin


    View profile
    Copy link
      Report member
    • 4y

    Hello, How can i achieve this with sqlite?

Please sign up or log in to reply