[FilterScript] [FS] Saving player position and loading it on the next login
#1

This is a simple way to save player position after closing the game and loading it after the player gets back.

First include these files (It will be attached with the script)

Код:
#include <Dini>
#include <dudb>
and then copy this to the end of the script.

Код:
Player(playerid)
{
 new player[MAX_PLAYER_NAME];
 GetPlayerName(playerid, player, sizeof(player));
 return player;
}
Second declare these three variables as a global variables in the top of your script,

Код:
new Float:positionx;
new Float:positiony;
new Float:positionz;
Float for real numbers or decimal numbers.

Now we need to open a file on scriptfiles to save our coordinates, to do that put this code at the end of your script.

Код:
FileStats(playerid)
{
  new a[256]; format(a, sizeof(a), "%s.ini",udb_encode(Player(playerid)));
  return a;
}
This will open a file in scriptfiles with the player name.

Now we need to store these positions in the file, to do that type this under the above code.

Код:
SaveStats(playerid)
{
  dini_IntSet(FileStats(playerid), "PositionX", floatround(positionx));
  dini_IntSet(FileStats(playerid), "PositionY", floatround(positiony));
  dini_IntSet(FileStats(playerid), "PositionZ", floatround(positionz));
}
This will store positionx as PositionX = the x position of the player and so on.

Next is to load the positions from the file, to do that type under the above code,

Код:
LoadStats(playerid)
{
  positionx = dini_Int(FileStats(playerid), "PositionX");
  positiony = dini_Int(FileStats(playerid), "PositionY");
  positionz = dini_Int(FileStats(playerid), "PositionZ");
}
This will make the positions saved in the file equals to the positions the player will teleport to when he/she login.

Ok now we need to load the file when the player connect, so we type this under OnPlayerConnect

Код:
LoadStats(playerid);
Ofcourse we need to save the positions in the file when the player disconnect, so we type this under OnPlayerDisconnect

Код:
new Float:x;
new Float:y;
new Float:z;

GetPlayerPos(playerid,x,y,z);
positionx = x;
positiony = y;
positionz = z;
if (!dini_Exists(FileStats(playerid)))
{
	dini_Create(FileStats(playerid));
}

SaveStats(playerid);
variables x, y and z is the current player position when the command GetPlayerPos used, so we make the positions of the file equal the one of the player.

Now we need to set the player position when he/she enters so we copy this code under OnPlayerSpawn,

Код:
SetPlayerPos(playerid,positionx,positiony,positionz);
Finally we need to set the player position when he/she dies so we have to copy this code under OnPlayerDeath

Код:
SetPlayerPos(playerid,1182.7733,-1323.6193,13.5785);
positionx = 1182.7733;
positiony = -1323.6193;
positionz = 13.5785;
This will set the player to all saints los santos hospital, ofcourse you can change the position of the place where you want your player to spawn when he/she dies. If you don't put this code the player will keep spawning in the loaded positions in the file.

Ok Now the script will look like:

Код:
#include <Dini>
#include <dudb>

new Float:positionx;
new Float:positiony;
new Float:positionz;

public OnPlayerConnect(playerid)
{
	LoadStats(playerid);
	return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
	new Float:x;
	new Float:y;
	new Float:z;

	GetPlayerPos(playerid,x,y,z);
	positionx = x;
	positiony = y;
	positionz = z;
	if (!dini_Exists(FileStats(playerid)))
	{
		dini_Create(FileStats(playerid));
	}

	SaveStats(playerid);
	return 1;
}

public OnPlayerSpawn(playerid)
{
	SetPlayerPos(playerid,positionx,positiony,positionz);
	return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
	SetPlayerPos(playerid,1182.7733,-1323.6193,13.5785);
	positionx = 1182.7733;
	positiony = -1323.6193;
	positionz = 13.5785;
	return 1;
}

Player(playerid)
{
 new player[MAX_PLAYER_NAME];
 GetPlayerName(playerid, player, sizeof(player));
 return player;
}

FileStats(playerid)
{
  new a[256]; format(a, sizeof(a), "%s.ini",udb_encode(Player(playerid)));
  return a;
}

SaveStats(playerid)
{
  dini_IntSet(FileStats(playerid), "PositionX", floatround(positionx));
  dini_IntSet(FileStats(playerid), "PositionY", floatround(positiony));
  dini_IntSet(FileStats(playerid), "PositionZ", floatround(positionz));
}

LoadStats(playerid)
{
  positionx = dini_Int(FileStats(playerid), "PositionX");
  positiony = dini_Int(FileStats(playerid), "PositionY");
  positionz = dini_Int(FileStats(playerid), "PositionZ");
}
For those who need it for GF Edits, I included it with a full working pure gf Edit. I downloaded the origional gf edit and added my script to it, just run it and test it.

Here is the gf edit with full server included with my save position script.
Download From Here
http://discoveryweb.net/savepositiongf.zip

Here is the full working script with the included files.
Download From Here
http://discoveryweb.net/saveposition.zip

Tell me if you need any help or if the link is broken.

Saleh Yassin
Reply
#2

Good Work
verry usefull.

Gr Artix
Reply
#3

Yeah!. Good job
Reply
#4

Wont work, getting the players position on disconnect will return X 0 Y 0 Z 0
Reply
#5

its only if player is leaving the server or if player is crashed?
Reply
#6

no, OnPlayerDisconnect is called AFTER the player disconnects so it becomes a invalid id
Reply
#7

Good work ! But i have another problem.

I don't want my player respawn at the LS hospital, but at its spawn. With 1 player class there is no problem, but when i tried to add the 2nd player class it shown me some errors.

Код:
C:\Users\isasteppe\Desktop\ServeurGTA\filterscripts\saveposition.pwn(76) : error 029: invalid expression, assumed zero
C:\Users\isasteppe\Desktop\ServeurGTA\filterscripts\saveposition.pwn(83) : error 029: invalid expression, assumed zero
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.
Here is the modificated script :
Код:
#include <a_samp>
#include <Dini>
#include <dudb>

new Float:positionx;
new Float:positiony;
new Float:positionz;


main()
{
	print("\n----------------------------------");
	print("Test");
	print("----------------------------------\n");
}

public OnGameModeInit()
{
	SetGameModeText("Test");
	AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
	AddPlayerClass(12,1958.3783, 1343.1572, 35.0000, 269.1425, 0, 0, 0, 0, 0, 0);
	return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
	printf("classid: %d", classid);
	switch (classid) {
		case 0:
			{
			GameTextForPlayer(playerid, "classe 0", 1000, 3);
			}
		case 1:
			{
			GameTextForPlayer(playerid, "classe 1", 1000, 3);
			}
	  }
  return 1;
}

public OnPlayerConnect(playerid)
{
	LoadStats(playerid);
	return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
	new Float:x;
	new Float:y;
	new Float:z;

	GetPlayerPos(playerid,x,y,z);
	positionx = x;
	positiony = y;
	positionz = z;
	if (!dini_Exists(FileStats(playerid)))
	{
		dini_Create(FileStats(playerid));
	}

	SaveStats(playerid);
	return 1;
}

public OnPlayerSpawn(playerid)
{
	SetPlayerPos(playerid,positionx,positiony,positionz);
	return 1;
}


//----- а modifier pour faire varier en fonction des spawns
public OnPlayerDeath(playerid, killerid, reason)
{
	if (case == 0) {
	SetPlayerPos(playerid,1958.3783, 1343.1572, 15.3746);
	positionx = 1958.3783;
	positiony = 1343.1572;
	positionz = 15.3746;
	}
	
	else if (case == 1) {
	SetPlayerPos(playerid,1958.3783, 1343.1572, 35.0000);
	positionx = 1958.3783;
	positiony = 1343.1572;
	positionz = 35.0000;
	}
	return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/kill", cmdtext, true, 10) == 0)
	{
		SetPlayerHealth(playerid,0);
		return 1;
	}
	return 0;
}

Player(playerid)
{
 new player[MAX_PLAYER_NAME];
 GetPlayerName(playerid, player, sizeof(player));
 return player;
}

FileStats(playerid)
{
  new a[256]; format(a, sizeof(a), "%s.ini",udb_encode(Player(playerid)));
  return a;
}

SaveStats(playerid)
{
  dini_IntSet(FileStats(playerid), "PositionX", floatround(positionx));
  dini_IntSet(FileStats(playerid), "PositionY", floatround(positiony));
  dini_IntSet(FileStats(playerid), "PositionZ", floatround(positionz));
}

LoadStats(playerid)
{
  positionx = dini_Int(FileStats(playerid), "PositionX");
  positiony = dini_Int(FileStats(playerid), "PositionY");
  positionz = dini_Int(FileStats(playerid), "PositionZ");
}
Btw i'm a newbie in scripting (started some days ago), i can have typed wrong things.
Reply
#8

Quote:
Originally Posted by Alice[WS
]
Good work ! But i have another problem.

I don't want my player respawn at the LS hospital, but at its spawn. With 1 player class there is no problem, but when i tried to add the 2nd player class it shown me some errors.

Код:
C:\Users\isasteppe\Desktop\ServeurGTA\filterscripts\saveposition.pwn(76) : error 029: invalid expression, assumed zero
C:\Users\isasteppe\Desktop\ServeurGTA\filterscripts\saveposition.pwn(83) : error 029: invalid expression, assumed zero
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.
Here is the modificated script :
----------
Btw i'm a newbie in scripting (started some days ago), i can have typed wrong things.
What exactly are you trying to do when the play dies. Are you trying to make the player respawn in a random position after he/she dies. If thats what you are trying to do then you don't have to use cases. And why do you have case 0 and case 1 in the OnPlayerRequestClass. You need to put something like that in OnPlayerRequestClass

Код:
	SetPlayerInterior(playerid, 0);
	SetPlayerPos(playerid,512.8511,-17.5289,1001.5653);
	SetPlayerFacingAngle(playerid, 328.6663);
	SetPlayerCameraPos(playerid,514.8894,-14.1297,1001.5653+1);
	SetPlayerCameraLookAt(playerid,512.8511,-17.5289,1001.5653);
	ApplyAnimation(playerid,"DANCING","dnce_M_b",4.0,1,0,0,0,-1);
	SetPlayerInterior(playerid, 3);
Remove the one you got. And the problem should be fixed.
Reply
#9

Quote:
Originally Posted by яυαяαι
Wont work, getting the players position on disconnect will return X 0 Y 0 Z 0
What do you mean it won't work. I had it on my server for years. Check your code very carefully and also check if there is a file in the scriptfiles.
Reply
#10

Quote:
Originally Posted by яυαяαι
no, OnPlayerDisconnect is called AFTER the player disconnects so it becomes a invalid id
Thats why I made a new variables, x,y and z so the position values will becomes equal to the player position witch has been returned by the command GetPlayerPos(playerid,x,y,z); and then it saves positionx,positiony,and positionz in the file.

Reply
#11

Good !
Reply
#12

As i said OnPlayerDisconnect is called AFTER the player disconnects so trying to get the players position wont work, it will just come out as
X: 0 Y: 0 Z: 0
Reply
#13

Can someone make script who works for GF edits? Im learning scripting, and thats hard from me.
Reply
#14

Quote:
Originally Posted by яυαяαι
As i said OnPlayerDisconnect is called AFTER the player disconnects so trying to get the players position wont work, it will just come out as
X: 0 Y: 0 Z: 0
Have you tryed the script before you speak or just from the look you decide. I am not the one who mad that command OnPlayerDisconnect.
If sa-mp know that this will not work for anything then they wouldn't of made such a command. Go learn pawno and then come speak. It does work, and the excute very fast so when the player type /q or /quit the command will excute.
Reply
#15

this is nice so for those who cant do this should use this as i see so many people asking how to do this. but glads someones made it.
Reply
#16

doesnt work for me or it works but woutn save.
Made a corinanate file also but everytime il spawn in normal spawn not the place where i loged out
Made this cordinates:
Код:
PositionX=1565
PositionY=-2294
PositionZ=14
Reply
#17

Quote:
Originally Posted by salehyassin
Quote:
Originally Posted by яυαяαι
As i said OnPlayerDisconnect is called AFTER the player disconnects so trying to get the players position wont work, it will just come out as
X: 0 Y: 0 Z: 0
Have you tryed the script before you speak or just from the look you decide. I am not the one who mad that command OnPlayerDisconnect.
If sa-mp know that this will not work for anything then they wouldn't of made such a command. Go learn pawno and then come speak. It does work, and the excute very fast so when the player type /q or /quit the command will excute.
Yes, i have tested it and it is IMPOSSIBLE for you to know when a player is going to disconnect!
And i do know pawno, so stop saying i don't.
Reply
#18

what about if player is in a interior and the player exits?
Reply
#19

I need filterscript with position save, who works with GF edits, or someone can help me with adding that script in my edited GF, PM me for details, i can pay for that too.
Reply
#20

Quote:
Originally Posted by Mauka4
I need filterscript with position save, who works with GF edits, or someone can help me with adding that script in my edited GF, PM me for details, i can pay for that too.
Just add the codes I said at my post. It will work.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)