[HELP] Saving/Restoring location
#1

Hi, I'm attempting to make the game respawn me from the save location upon quitting the game. However, whenever I quit it saves the location fine, but does not automatically logon.

It pops up the login box, I enter my password, and then it sits there with just <<, >> and Spawn.

Could anybody help me with my code and let me know why it doesnt automatically force the player to spawn at the location and set his class?

Код:
enum pEnum
{
	Name[MAX_PLAYER_NAME],
	Password[34],
	Admin,
	Money,
	Float:X,
	Float:Y,
	Float:Z
};
new UserStats[MAX_PLAYERS][pEnum];

public OnPlayerRequestClass(playerid, classid)
{
	TogglePlayerSpectating(playerid, 0);
    SetTimer("AutoSpawn",500,false);// 2 seconds it will spawn u
    return 1;
}
forward AutoSpawn(playerid);
public AutoSpawn(playerid)
{
    new Query[128],pName[24],Load[50];
    GetPlayerName(playerid,pName,sizeof(pName));
	format(Query,sizeof(Query), "SELECT `PosX`, `PosY`, `PosZ` FROM `Users` WHERE `Username` = '%s'", pName);
	mysql_query(Query);
	mysql_store_result();
	if(mysql_num_rows() != 0 && mysql_fetch_row(Load))
	{
	    SpawnPlayer(playerid);
		sscanf(Load, "p<|>fff", UserStats[playerid][X], UserStats[playerid][Y], UserStats[playerid][Z]);
		SetPlayerPos(playerid, UserStats[playerid][X], UserStats[playerid][Y], UserStats[playerid][Z]);
 		SetPlayerSkin(playerid, 230);
	}
	mysql_free_result();
    return 1;
}
Reply
#2

Where is your DialogResponse code for the login box.
Reply
#3

Here is all of my script, i know its a bit messy but im still learning this coding language.

Код:
#include <a_samp>
#include <a_mysql>
#include <sscanf2>
#include <core>
#include <float>

//#include "../include/gl_spawns.inc"

#define green 0x33AA33AA
#define red 0xFF0000AA

#define mysql_host				"127.0.0.1"
#define mysql_user				"XXXXXXXXXXX"
#define mysql_password			"XXXXXXXXXXX"
#define mysql_database			"samp"

#pragma tabsize 0

main()
{
	print("\n----------------------------------");
	print("  Welcome to UK-MP GTA Server!\n");
	print("----------------------------------\n");
}

public OnPlayerConnect(playerid)
{
	GameTextForPlayer(playerid,"~w~SA-MP: ~r~UK-MP GTA Server",5000,5);
	new Query[80],pName[24],string[128];
	GetPlayerName(playerid,pName,24);
	format(Query,sizeof(Query),"SELECT `Username` FROM `Users` WHERE `Username` = '%s' LIMIT 1;",pName);
	mysql_query(Query);
	mysql_store_result();
	if(mysql_num_rows() != 0)
	{
	    format(string,sizeof(string),"Hey, %s! \nYour account is registered.\nPlease enter the password to log in!",pName);
	    ShowPlayerDialog(playerid,0,DIALOG_STYLE_INPUT,"Log in",string,"Login","");
	}
	else
	{
	    format(string,sizeof(string),"Hey, %s! \nYour account is not registered. \nPlease register to continue!",pName);
	    ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Register",string,"Register","");
	}
	mysql_free_result();
	return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
  new Query[200],pName[24];
  new Float:X, Float:Y, Float:Z;
  GetPlayerPos(playerid, X, Y, Z);
  GetPlayerName(playerid, pName, sizeof(pName));
  format(Query,sizeof(Query),"UPDATE `Users` SET PosX='%f',PosY='%f',PosZ='%f',Money='%i' WHERE `Username` = '%s';",X,Y,Z,GetPlayerMoney(playerid),pName);
  mysql_query(Query);
  return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(dialogid == 1)
    	{
        	if(strlen(inputtext) == 0)
    		{
            	ShowPlayerDialog(playerid,0,DIALOG_STYLE_INPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password for it! \n","Register!","");
        	}
        	else
        	{
            	new EscapedText[60], Query[200], pName[24];
            	mysql_real_escape_string(inputtext, EscapedText);
            	GetPlayerName(playerid,pName,24);
            	format(Query,sizeof(Query),"INSERT INTO `Users` (Username,Password,Money,BankMoney,Level,PosX,PosY,PosZ,CarID1,CarID2,HouseID,GarageID) VALUES ('%s','%s','5000','0','1','2495.0720','-1687.5278','13.5150','0','0','0','0');",pName);
            	mysql_query(Query);
            	SendClientMessage(playerid,green,"You have been successfully registered!");
            	GivePlayerMoney(playerid,5000);
            	SetPlayerScore(playerid,1);
        	}
    	}
	if(dialogid == 0)
    {
        if(strlen(inputtext) == 0)
        {
            ShowPlayerDialog(playerid,0,DIALOG_STYLE_INPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password for it! \n","Register!","");
        }
        else
        {
            LoginPlayer(playerid,inputtext);
        }
    }
    return 1;
}

stock LoginPlayer(playerid,const password[])
{
    new EscapedText[60], Query[128], pName[24];
    mysql_real_escape_string(password, EscapedText);
    GetPlayerName(playerid,pName,24);
    format(Query,sizeof(Query),"SELECT * FROM `Users` WHERE `Username` = '%s' AND `Password` = '%s';",pName,EscapedText);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_num_rows() != 0)
    {
        SendClientMessage(playerid,green,"You have been logged in!");
        LoadStats(playerid);
    }
    else
    {
            SendClientMessage(playerid,red,"Wrong password!");
        Kick(playerid);
    }
    mysql_free_result();
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	new idx;
	new cmd[256];
	
	cmd = strtok(cmdtext, idx);

	if(strcmp(cmd, "/stats", true) == 0) {
    	SendClientMessage(playerid,red,"[Money: $GetPlayerMoney(playerid)]");
	}

	return 0;
}

public OnPlayerSpawn(playerid)
{
	SetPlayerInterior(playerid,0);
	TogglePlayerClock(playerid,0);
	return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
   	return 1;
}

enum pEnum
{
	Name[MAX_PLAYER_NAME],
	Password[34],
	Admin,
	Money,
	Float:X,
	Float:Y,
	Float:Z
};
new UserStats[MAX_PLAYERS][pEnum];

public OnPlayerRequestClass(playerid, classid)
{
	TogglePlayerSpectating(playerid, 0);
    SetTimer("AutoSpawn",500,false);// 2 seconds it will spawn u
    return 1;
}
forward AutoSpawn(playerid);
public AutoSpawn(playerid)
{
    new Query[128],pName[24],Load[50];
    GetPlayerName(playerid,pName,sizeof(pName));
	format(Query,sizeof(Query), "SELECT `PosX`, `PosY`, `PosZ` FROM `Users` WHERE `Username` = '%s'", pName);
	mysql_query(Query);
	mysql_store_result();
	if(mysql_num_rows() != 0 && mysql_fetch_row(Load))
	{
	    SpawnPlayer(playerid);
		sscanf(Load, "p<|>fff", UserStats[playerid][X], UserStats[playerid][Y], UserStats[playerid][Z]);
		SetPlayerPos(playerid, UserStats[playerid][X], UserStats[playerid][Y], UserStats[playerid][Z]);
 		SetPlayerSkin(playerid, 230);
	}
	mysql_free_result();
    return 1;
}



enum PlayerInfo
{
    Username[23],
    Password[24],
    Money
}
new PInfo[MAX_PLAYERS][PlayerInfo];

stock LoadStats(playerid)
{
    new pName[24],Query[80];
    GetPlayerName(playerid,pName,24);
    format(Query, sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s';", pName);
    mysql_query(Query);
    mysql_store_result();
    mysql_fetch_row_format(Query, "|");
    sscanf(Query, "e<p<|>s[24]s[23]i>", PInfo[playerid]);
    mysql_free_result();
    GivePlayerMoney(playerid,PInfo[playerid][Money]);
    return 1;
}

public OnGameModeInit()
{
	//AllowAdminTeleport(1);
	mysql_connect(mysql_host,mysql_user,mysql_database,mysql_password);
	mysql_debug(1);
	SetGameModeText("UK-MP");
	ShowPlayerMarkers(1);
	ShowNameTags(1);

	AddPlayerClass(265,1958.3783,1343.1572,15.3746,270.1425,0,0,0,0,-1,-1);

	return 1;
}

strtok(const string[], &index)
{
	new length = strlen(string);
	while ((index < length) && (string[index] <= ' '))
	{
		index++;
	}

	new offset = index;
	new result[20];
	while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
	{
		result[index - offset] = string[index];
		index++;
	}
	result[index - offset] = EOS;
	return result;
}
Reply
#4

pawn Код:
stock LoadStats(playerid)
{
    new pName[24],Query[80];
    GetPlayerName(playerid,pName,24);
    format(Query, sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s';", pName);
    mysql_query(Query);
    mysql_store_result();
    mysql_fetch_row_format(Query, "|");
    sscanf(Query, "e<p<|>s[24]s[23]i>", PInfo[playerid]);
    mysql_free_result();
    GivePlayerMoney(playerid,PInfo[playerid][Money]);
    SetSpawnInfo(playerid, 0, 297, UserStats[playerid][X], UserStats[playerid][Y], UserStats[playerid][Z], 10, 0,0,0,0,0,0);
    SpawnPlayer(playerid);
    return 1;
}
Try this.
Reply
#5

I have another problem with mine atm too.

The mysql query to INSERT a new user isnt functioning properly aswell.

It is not picking up the password from the dialog box and is missing the ; off the end.

This is the code:
Код:
	if(dialogid == 1)
    	{
        	if(strlen(inputtext) == 0)
    		{
            	ShowPlayerDialog(playerid,0,DIALOG_STYLE_INPUT,"Register - Enter your password","You are about to register a new account! \nPlease choose the password for it! \n","Register!","");
        	}
        	else
        	{
            	new EscapedText[60], Query[200], pName[24];
            	mysql_real_escape_string(inputtext, EscapedText);
            	GetPlayerName(playerid,pName,24);
            	format(Query,sizeof(Query),"INSERT INTO `Users` (Username,Password,Money,BankMoney,Level,PosX,PosY,PosZ,CarID1,CarID2,HouseID,GarageID) VALUES ('%s','%s','5000','0','1','2495.0720','-1687.5278','13.5150','0','0','0','0');",pName,EscapedText);
            	mysql_query(Query);
            	SendClientMessage(playerid,green,"You have been successfully registered!");
            	GivePlayerMoney(playerid,5000);
            	SetPlayerScore(playerid,1);
        	}
    	}
[16:28:57] CMySQLHandler::Query(INSERT INTO `Users` (Username,Password,Money,BankMoney,Level,PosX,PosY ,PosZ,CarID1,CarID2,HouseID,GarageID) VALUES ('Test_Account','','5000','0','1','2495.0720','-1687.5278','13.5150','0','0','0','0') - An error has occured. (Error ID: 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1)
Reply
#6

Sorry, but I have no experience with MySql, I use y_ini.
Reply
#7

Nvm, I fixed that, it was because Query[200] was too short. I had to make it accept a few more characters.

Okay, when an existing player joins the server using your command above, the spawn box disappears and I spawn, but im falling under the world, after a few seconds I'm spawned on top of the world. It doesnt seem to be using the co-ords I have provided.
Reply
#8

having problems with this at the moment, im guessing it is because of the double mysql query's
Код:
stock LoadStats(playerid)
{
    new pName[24],Query[80],GetLoc[80];
    GetPlayerName(playerid,pName,24);
    format(Query,sizeof(Query), "SELECT * FROM `Users` WHERE `Username` = '%s';", pName);
    mysql_query(Query);
    mysql_store_result();
    mysql_fetch_row_format(Query, "|");
    sscanf(Query, "e<p<|>s[24]s[23]i>", PInfo[playerid]);
    mysql_free_result();
    format(GetLoc,sizeof(GetLoc), "SELECT `PosX`, `PosY`, `PosZ` FROM `Users` WHERE `Username` = '%s';", pName);
    mysql_query{GetLoc);
	mysql_store_result();
    mysql_fetch_row_format(GetLoc, "|");
    sscanf(Query2, "p<|>fff", UserStats[playerid][PosX], UserStats[playerid][PosY], UserStats[playerid][PosZ]);
    mysql_free_result();
    GivePlayerMoney(playerid,PInfo[playerid][Money]);
    SetSpawnInfo(playerid, 0, 230, UserStats[playerid][PosX], UserStats[playerid][PosY], UserStats[playerid][PosZ], 10, 0,0,0,0,0,0);
    SpawnPlayer(playerid);
    return 1;
}
It spits this up

Код:
C:\Users\Simon\Desktop\samp\gamemodes\bare.pwn(210) : error 028: invalid subscript (not an array or too many subscripts): "mysql_query"
C:\Users\Simon\Desktop\samp\gamemodes\bare.pwn(210) : warning 215: expression has no effect
C:\Users\Simon\Desktop\samp\gamemodes\bare.pwn(210) : error 001: expected token: ";", but found ")"
C:\Users\Simon\Desktop\samp\gamemodes\bare.pwn(210) : error 029: invalid expression, assumed zero
C:\Users\Simon\Desktop\samp\gamemodes\bare.pwn(210) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


4 Errors.
Reply
#9

This is an example result string which is returned from mysql.

Would someone help me write my sscanf query please?

Result from MySQL:
Testing_Username|test|9999|10430|1|131.941955|-67.295921|1.578125|1204|1308|948|744

9999 = Money
10430 = Bank
1 = Level
131.941955 = X
-67.295921 = Y
1.578125 = Z
1204 = CarID1
1308 = CarID2
948 = HouseID
744 = GarageID

I need to adapt this to read all information from my database

The string below returns the Username, Password and Money perfectly but I'm unsure about how to code the others.

Код:
    sscanf(Query, "e<p<|>s[24]s[23]i>", PInfo[playerid]);
this is my PInfo stuff:

Код:
enum PlayerInfo
{
    Username[23],
    Password[24],
    Money,
    Bank,
    Level,
	Float:PosX,
	Float:PosY,
	Float:PosZ,
	CarID1,
	CarID2,
	HouseID,
	GarageID
}
new PInfo[MAX_PLAYERS][PlayerInfo];
Reply
#10

Would anybody be able to help me please?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)