SA-MP Forums

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

Thread Tools Display Modes
Old 01/04/2020, 11:50 AM   #1
Huge Clucker
Join Date: Oct 2010
Posts: 307
Reputation: 11
Default Variable saving


I found this perfect system for accounts in SA:MP by pBlueG. Since I am total newbie in MySQL, I tried to read the code and understand, how it is crafted and why certain functions behave how they do, when I implemented the script into my localhost, it worked great, but when I tried to add new variables, it didnt work as I would expect.

To all the existing ones, I wanted to add a variable for admin permissions called "Level", so I used variable "Interior" as an example, so I did following changes:

Added it into the variable enumerator:
	Float: X_Pos,
	Float: Y_Pos,
	Float: Z_Pos,
	Float: A_Pos,
Then into the assigning function:
	cache_get_value_int(0, "id", Player[playerid][ID]);
	cache_get_value_int(0, "kills", Player[playerid][Kills]);
	cache_get_value_int(0, "deaths", Player[playerid][Deaths]);
	cache_get_value_float(0, "x", Player[playerid][X_Pos]);
	cache_get_value_float(0, "y", Player[playerid][Y_Pos]);
	cache_get_value_float(0, "z", Player[playerid][Z_Pos]);
	cache_get_value_float(0, "angle", Player[playerid][A_Pos]);
	cache_get_value_int(0, "interior", Player[playerid][Interior]);
	cache_get_value_int(0, "level", Player[playerid][Level]);
	return 1;
Next step was to save it, when player disconnects. In the script, there is function UpdatePlayerData being executed when players quits the game, so I added it there like this:
UpdatePlayerData(playerid, reason)
	if (Player[playerid][IsLoggedIn] == false) return 0;
	if (reason == 1)
		GetPlayerPos(playerid, Player[playerid][X_Pos], Player[playerid][Y_Pos], Player[playerid][Z_Pos]);
		GetPlayerFacingAngle(playerid, Player[playerid][A_Pos]);
	new query[145];
	mysql_format(g_SQL, query, sizeof query, "UPDATE `players` SET `x` = %f, `y` = %f, `z` = %f, `angle` = %f, `interior` = %d, `level` = %d WHERE `id` = %d LIMIT 1", Player[playerid][X_Pos], Player[playerid][Y_Pos], Player[playerid][Z_Pos], Player[playerid][A_Pos], GetPlayerInterior(playerid), Player[playerid][Level], Player[playerid][ID]);
	mysql_tquery(g_SQL, query);
	return 1;
In the script, there is a function that creates a table in database, if it is not existing. Since I will be adding variables even after the table will be created, I did not add it in there, i kept it native:
	mysql_tquery(g_SQL, "CREATE TABLE IF NOT EXISTS `players` (`id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(24) NOT NULL,`password` char(64) NOT NULL,`salt` char(16) NOT NULL,`kills` mediumint(8) NOT NULL DEFAULT '0',`deaths` mediumint(8) NOT NULL DEFAULT '0',`x` float NOT NULL DEFAULT '0',`y` float NOT NULL DEFAULT '0',`z` float NOT NULL DEFAULT '0',`angle` float NOT NULL DEFAULT '0',`interior` tinyint(3) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`))");
	return 1;
What I did instead, was that I created a table by hand in the Phpmyadmin interface, i setted it to the same settings as interior variable, so it looked like this:

The problem however was, or well is, that after I registered a account, setted the variable level (ingame) to 5 and quit the game, the variable did not save to the database. Is there any way, this could be fixed? For any adivces I am going to be more, than thankful.
_GHT_MarK445 is offline   Reply With Quote
Old 01/04/2020, 12:00 PM   #2
Join Date: May 2018
Posts: 1,227
Reputation: 205
Default Re: Variable saving

First of all, check mysql logs.

Is the 0 shown in the picture a default value? If it is, the register part is alright. You will also need to increase the size of `query` variable in `UpdatePlayerData` function which is likely the problem because you added more text.

But since you are new, update database when something does not change very frequently the moment you set a new value. What I mean is when you promote/demote a player, assign to `Player[target_id_here][Level]` and also update the level in database so you will not have to update everything on disconnect even when they never changed. Remove LIMIT 1 from the UPDATE query too because it is not needed.
Calisthenics is offline   Reply With Quote
Old 01/04/2020, 12:46 PM   #3
Huge Clucker
Join Date: Oct 2010
Posts: 307
Reputation: 11
Default Re: Variable saving

Where can I find those logs?

Yes, on the picture is default value, however in the tree (where I see all of them), the value is real-time.

When it comes to the size of the variable, it did not help. The actual size right now after I added the Level variable is 118.
_GHT_MarK445 is offline   Reply With Quote
Old 01/04/2020, 12:55 PM   #4
Huge Clucker
MrCesar's Avatar
Join Date: Sep 2016
Location: Russia
Posts: 290
Reputation: 1
Default Re: Variable saving

The logs would be in the log-core folder in your server folder, you have warning and error logs
MrCesar is offline   Reply With Quote

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
problem saving variable saving is wrong unamed Scripting Help 1 31/03/2015 09:26 AM
Saving different IDs in the same variable. Denying Scripting Help 2 07/03/2013 12:56 PM
Saving age Variable || A little help please. Jaber_Brown Scripting Help 5 15/08/2012 08:43 PM
Saving my variable? lawonama Help Archive 10 09/07/2011 06:13 PM

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

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