Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

Super-Moderator
Staff member
Super-Moderator
Joined
Apr 28, 2007
Messages
1,498
Reaction score
756
I think this tutorial will be useful for many people. Especially for the new "coders". I added this spawn location save to my own RPG Gamemode. So it's working, don't worry.

Tutorial:

Step 0.
We will have to define dini, so we can use it.
Add this line at the top of your script, below #include <a_samp> :
Code:
#include <dini>
Done? Head to step 1.

Step 1.
Let's use the public function public OnPlayerDisconnect(playerid, reason).
Find that function. CTRL+F.
If you found it, we can continue.
First we will have to define some things.
Add this at the top of the public function:
Code:
new file[128], pname[MAX_PLAYER_NAME];
new Float:x, Float:y, Float:z;

Step 2.
Now we will have to make clear what pname is. And we will have to make sure a file will be created to save the data.
Add this below the line we added at Step 1.
Code:
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "[URL="file://\\SavePos\\%s.ini"]\\SavePos\\%s.ini[/URL]", pname);

Step 3.
We made the "script" clear it will have to load/use a file called "playername.ini" for the data. But it doesn't know if that file exists yet. If it exists, it will save the data. If it doesn't exist, it won't save the data. So, let's make it add the file if it doesn't exist.
Add this below the lines we added at Step 2.
Code:
if(!dini_Exists(file))
dini_Create(file);

PAUSE
Let's check the script.
This is what you currently have:
Code:
public OnPlayerDisconnect(playerid, reason)
{
public OnPlayerDisconnect(playerid, reason)
{
new file[128], pname[MAX_PLAYER_NAME];
new Float:x, Float:y, Float:z;
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "[URL="file://\\SavePos\\%s.ini"]\\SavePos\\%s.ini[/URL]", pname);
if(!dini_Exists(file))
dini_Create(file);
return 1;
}

If everything is right, head to Step 4.

Step 4.
We will have to get the player position of the player.
We can do this by adding this line below dini_create(file);
Add this line below dini_create(file);:
Code:
GetPlayerPos(playerid, x, y, z);
Done? Go to Step 5.
Don't worry about the
"return 1;
}".

Step 5.
Now we will have to make it set the X,Y,Z position in the "username.ini" file.
We can do that by adding this below the line we added at Step 4.
Code:
dini_FloatSet(file, "posX", x);
dini_FloatSet(file, "posY", y);
dini_FloatSet(file, "posZ", z);
This will set the X, Y, and Z position of the player.

Now we are done with the public function OnPlayerDisconnect.
We are not done with the script! We still have to make the player spawn at the saved location.

PAUSE
This is how the function should look like!
Code:
public OnPlayerDisconnect(playerid, reason)
{
new file[128], pname[MAX_PLAYER_NAME];
new Float:x, Float:y, Float:z;
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "[URL="file://\\SavePos\\%s.ini"]\\SavePos\\%s.ini[/URL]", pname);
if(!dini_Exists(file))
dini_Create(file);
GetPlayerPos(playerid, x, y, z);
dini_FloatSet(file, "posX", x);
dini_FloatSet(file, "posY", y);
dini_FloatSet(file, "posZ", z);
return 1;
}

Is everything right?
Head to Step 6.

Step 6.
We will have to use the public function "OnPlayerSpawn(playerid)".
For some of you this function exists, for some of you it doesn't.
Let's find this function. CTRL + F then type "OnPlayerSpawn". If you can't find it, add it manually:
Code:
public OnPlayerSpawn(playerid)
{
return 1;
}

If you have this function, add this line below the bracket "{".
We will have to define some things, again. I will not explain these lines one by one, we already used them at the OnPlayerDisconnect function.
Code:
new file[128], pname[MAX_PLAYER_NAME];
new Float:x, Float:y, Float:z;
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "[URL="file://\\SavePos\\%s.ini"]\\SavePos\\%s.ini[/URL]", pname);

Done? head to Step 7.

Step 7.
This will be new for you.
We will have to make the script look for the lines "posX, posY, posZ".
We can do that by adding these lines below the liens we added at Step 6.
Code:
x = dini_Float(file, "posX");
y = dini_Float(file, "posY");
z = dini_Float(file, "posZ");
Done? Head to Step 8.

Step 8.
Okay, the script checked for these lines. Now we will have to make the player spawn at the coordinates we saved.
Add this line below the latest line we added at Step 7.
Code:
SetPlayerPos(playerid, x, y, z);

We are ALMOST done.
We're done with the script, but we still have to do something.

This is how the script should look like:

Code:
public OnPlayerDisconnect(playerid, reason)
{
new file[128], pname[MAX_PLAYER_NAME];
new Float:x, Float:y, Float:z;
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "[URL="file://\\SavePos\\%s.ini"]\\SavePos\\%s.ini[/URL]", pname);
if(!dini_Exists(file))
dini_Create(file);
GetPlayerPos(playerid, x, y, z);
dini_FloatSet(file, "posX", x);
dini_FloatSet(file, "posY", y);
dini_FloatSet(file, "posZ", z);
return 1;
}
public OnPlayerSpawn(playerid)
{
new file[128], pname[MAX_PLAYER_NAME];
new Float:x, Float:y, Float:z;
GetPlayerName(playerid, pname, sizeof(pname));
format(file, sizeof(file), "[URL="file://\\SavePos\\%s.ini"]\\SavePos\\%s.ini[/URL]", pname);
x = dini_Float(file, "posX");
y = dini_Float(file, "posY");
z = dini_Float(file, "posZ");
SetPlayerPos(playerid, x, y, z);
return 1;
}

Now, go to Step 9.

Step 9.
The file wants to create at the folder "SavePos". But that folder doesn't exist. So we will have to add it manually.
Go to your scriptfiles folder (Default: C:\Program Files\Rockstar Games\GTA San Andreas\Samp Server\scriptfiles).
Then add a new folder called:
SavePos.

Now we are done!

Thank you and good luck.
Questions? Problems? bugs? Post them here.
 
Last edited:
Skilled Illusionist
Joined
Apr 28, 2005
Messages
378
Reaction score
6
Great :) Pretty easy to follow, and useful.

Maybe it would be a good idea though to check if file exists at OnPlayerSpawn as well? In case player visits the server for the first time. If it doesn't then spawn at default coords.
 
Super-Moderator
Staff member
Super-Moderator
Joined
Apr 28, 2007
Messages
1,498
Reaction score
756
Great :) Pretty easy to follow, and useful.

Maybe it would be a good idea though to check if file exists at OnPlayerSpawn as well? In case player visits the server for the first time. If it doesn't then spawn at default coords.

You don't need to. Everyone has an "AddPlayerClass" function at the callback "OnGameModeInit".
 
Custom Title Activated
Loyal Member
Joined
Mar 26, 2009
Messages
1,778
Reaction score
1,209
Lol true noob friendly
 
Initiate Mage
Joined
Aug 2, 2023
Messages
1
Reaction score
0
maybe the code works but not the best on the first spawn it spawns me at 0.0,0.0,0.0 and when I die it spawns me at the last spawn location
but when I leave the relog, it spawns me fine in the same location where I exited
 
Back
Top