Author Topic: Saving/Loading Script[Solved]  (Read 3984 times)

Nvm, found the problem.
Code: [Select]
function addItem(%player,%image)
{
to
Code: [Select]
function addItem(%client,%image)
{
%player = %client.player;

I never declared the client in addItem()


I'm having a similar problem. I followed your advice and did the code but it still doesn't work.
Heres the code.

Quote
registerOutputEvent("Player", "addItem", "datablock ItemData", 1);

function Player::addItem(%player,%image,%client)
{
  %player = %client.player;
   for(%i = 0; %i < %player.getDatablock().maxTools; %i++)
   {
      %tool = %player.tool[%i];
      if(%tool == 0)
      {
         %player.tool[%i] = %image;
         %player.weaponCount++;
         messageClient(%client,'MsgItemPickup','',%i,%image);
         break;
      }
   }
}





function serverCmdsaveme(%client, %save)
{
%BLID = %client.bl_id;

messageClient(%client,'',"<color:FFFFFF>Save is <color:00FF00>Succesful<color:FFFFFF>!");
%file = new FileObject(); // Create a file object
%file.openForWrite("config/server/FalloutRPG/" @ %BLID @ ".txt"); // Open a file, if it exists, clear it. If it doesn't exist, create it.
%file.writeLine("" @ %client.player.curMoney);
%file.writeLine("" @ %client.exp);
%file.writeLine("" @ %client.needexp);
%file.writeLine(%client.player.position);
%file.writeLine(%client.player.tool[0]);
%file.writeLine(%client.player.tool[1]);
%file.writeLine(%client.player.tool[2]);
%file.writeLine(%client.player.tool[3]);
%file.writeLine(%client.player.tool[4]);
%file.close(); // close the file
%file.delete(); // delete the file object
}


function serverCmdloadme(%client, %save, %player, %image)
{
%player = %client.player;
%BLID = %client.bl_id;

messageClient(%client,'',"<color:FFFFFF>Load is <color:00FF00>Succesful<color:FFFFFF>!");
%file = new FileObject(); // Create a file object
%file.openForRead("config/server/FalloutRPG/" @ %BLID @ ".txt"); // Open a file to read
while(!%file.isEOF()){ // loop until we have reached the end of the file
%client.player.curMoney = %file.readLine();
%client.exp = %file.readLine();
%client.needexp = %file.readLine();
%client.player.cleartools();
%client.player.position = %file.readLine();
%client = %player.client;
addItem(%client.player.tool[0],%file.readLine());
addItem(%client.player.tool[1],%file.readLine());
addItem(%client.player.tool[2],%file.readLine());
addItem(%client.player.tool[3],%file.readLine());
addItem(%client.player.tool[4],%file.readLine());
%lines++;
}
%file.close();
%file.delete();

}

Anyone see a problem?

I'm having a similar problem. I followed your advice and did the code but it still doesn't work.
Heres the code.

Code: [Select]
registerOutputEvent("Player", "addItem", "datablock ItemData", 1);

function Player::addItem(%player,%image,%client)
{
  %player = %client.player;
   for(%i = 0; %i < %player.getDatablock().maxTools; %i++)
   {
      %tool = %player.tool[%i];
      if(%tool == 0)
      {
         %player.tool[%i] = %image;
         %player.weaponCount++;
         messageClient(%client,'MsgItemPickup','',%i,%image);
         break;
      }
   }
}





function serverCmdsaveme(%client, %save)
{
%BLID = %client.bl_id;

messageClient(%client,'',"<color:FFFFFF>Save is <color:00FF00>Succesful<color:FFFFFF>!");
%file = new FileObject(); // Create a file object
%file.openForWrite("config/server/FalloutRPG/" @ %BLID @ ".txt"); // Open a file, if it exists, clear it. If it doesn't exist, create it.
%file.writeLine("" @ %client.player.curMoney);
%file.writeLine("" @ %client.exp);
%file.writeLine("" @ %client.needexp);
%file.writeLine(%client.player.position);
%file.writeLine(%client.player.tool[0]);
%file.writeLine(%client.player.tool[1]);
%file.writeLine(%client.player.tool[2]);
%file.writeLine(%client.player.tool[3]);
%file.writeLine(%client.player.tool[4]);
%file.close(); // close the file
%file.delete(); // delete the file object
}


function serverCmdloadme(%client, %save, %player, %image)
{
%player = %client.player;
%BLID = %client.bl_id;

messageClient(%client,'',"<color:FFFFFF>Load is <color:00FF00>Succesful<color:FFFFFF>!");
%file = new FileObject(); // Create a file object
%file.openForRead("config/server/FalloutRPG/" @ %BLID @ ".txt"); // Open a file to read
while(!%file.isEOF()){ // loop until we have reached the end of the file
%client.player.curMoney = %file.readLine();
%client.exp = %file.readLine();
%client.needexp = %file.readLine();
%client.player.cleartools();
%client.player.position = %file.readLine();
%client = %player.client;
addItem(%client.player.tool[0],%file.readLine());
addItem(%client.player.tool[1],%file.readLine());
addItem(%client.player.tool[2],%file.readLine());
addItem(%client.player.tool[3],%file.readLine());
addItem(%client.player.tool[4],%file.readLine());
%lines++;
}
%file.close();
%file.delete();

}



Anyone see a problem?

First, thanks for using this topic instead of creating a new one. This is a prime example of an unlocked dead topic being useful.

Second, please don't bump threads that are on the first page already. Especially not by quoting your entire post. We aren't going to be more likely to help you because you posted your code twice. There's only maybe twenty people that regularly visit and help on this board, so be patient in the future while waiting for a response.

The problem is that you're calling addItem(%client.player.tool[_],%file.readLine()); Replace all of these with addItem(%client.player, %file.readLine()); and you should be golden.

I'd like to also warn you that the while loop in serverCmdLoadMe is entirely pointless, along with the last two arguments to that function.
« Last Edit: September 25, 2013, 05:24:32 PM by $trinick »

First, thanks for using this topic instead of creating a new one. This is a prime example of an unlocked dead topic being useful.

Second, please don't bump threads that are on the first page already. Especially not by quoting your entire post. We aren't going to be more likely to help you because you posted your code twice. There's only maybe twenty people that regularly visit and help on this board, so be patient in the future while waiting for a response.

At least locked topics aren't as bad as "nvm i fixed it" -> no follow up.

And to be fair, it looked like he tried to edit the post to change it to [code][/code] tags.

All it does is clear my inventory

At least locked topics aren't as bad as "nvm i fixed it" -> no follow up.
True, but that's a different issue all together. The reason I brought it up was because this was a topic that had previously been locked then unlocked for the benefit of the community.

And to be fair, it looked like he tried to edit the post to change it to [code][/code] tags.
I actually looked for a reason he may have accidentally clicked quote instead of edit, I missed that he changed it to code tags instead of quote. The content was the same so I assumed it was an exact quote.

All it does is clear my inventory
Did you change the addItem calls? Your serverCmdLoadMe function should look like this.

It crashed my game. Saying serverCmdLoadMe over and over again.

I don't know what you mean by that, but replace your function entirely with this version. I simply removed some redundancies and added a file check. There is nothing in that code that should crash your game.

It still just clears my inventory. Idk what the issue is D:

It still just clears my inventory. Idk what the issue is D:
Check config/server/FalloutRPG/yourblid.txt. Post its contents.

(NOTE: Blank is not actually that. its just blank.

"BlanK"
"BlanK"
"BlanK"
21.5587 9.71494 0.00188406
65
75
383
0
0

It still just clears my inventory. Idk what the issue is D:

From the code you've posted it looks like you declared addItem as a method for the Player class. To fix the issue you're having you will need to call that method on a player like so.

Code: [Select]
%player.addItem(%file.readLine(), %client);

Still doesnt work. Run the code yourself and see if you can get it to work.