Looking for some help with a addon for DarkRP

Hello im working on a DarkRP edit, and i try to do so your weapons is being saved, so you will spawn with you weapon everytime you join.

I have custom NPC shops on the map, and if you buy a weapon you are going to have the same weapon until you drop it. When you drop it/sell/delete, then the script is going to delete the saved weapons from the database…

Im not a proffesional Lua coder, but i know its not that big code to make, if some of you guys could help me a bit.

I tryied out to make it with the file.CreateDir() function and file.Write() function, but i totally failed :frowning:

So im begging you guys… PLEASE help me…

What do you need help with exactly?

If it is the data saving you could have their data saved within their table and glon.encode() it to a file with their uniqueID or something. When the player initially spawns, then you can load their data by checking their uniqueID against all the files within the diretory you saved it in.

Look im using the derp inventory system.

if you buy a weapon from the npc, and leaves the server and joins it again, i wan’t to load the weapon he bought into the inventory, so he doesent need to save money and buy it again. Do you understand ?

Sorry my bad english…

So you need to use a file.Write() or SQL saving system, then just do ply:Give() during spawn if their Steam ID matches your files.

Saving/loading is always a pain, but it’s a simple concept. What you do is, when the player buys from the NPC, you grab the weapon class that they bought, say a P228.

You would take this, and grab the user’s name and UniqueID as well, then write them to a text-file. Then, when the player reconnects, you look through your data folder for the file, read for the class inside, then give them that weapon.

I can’t really provide the CODE for it, college work right now, but you just need to walk through it one step at a time, and get each part of it working. Start with saving, then try loading, then make the whole script.

Thank you soooo much, it helped me, it were really simple thought, but as i said, i just started with Lua…

Thank you one more time :slight_smile: really helpful !

[editline]19th January 2012[/editline]

Just one more question to go. Do i need to put this in only serveside code? or anything in shared.lua or client side ?

The actual saving is done serverside. Depends on how the buy code was made, but all you’re doing is adding a couple lines to that.

Ok :slight_smile: I will try. Ill ask for help if i need anything else :slight_smile: Thanks !

Ok, i need some help here,

I tried to make this,

if file.Exists("wep_save/"..ply:UniqueID().."/weapon.txt") then 
local File = file.Read("wep_save/"..ply:UniqueID().."/weapon.txt")
ply:Give(File)
    else
file.CreateDir("wep_save/"..ply:UniqueID().."/weapon.txt")  
end

in the function
function GM:PlayerLoadout(ply)
end

but it doesent make the directory :frowning:
Please help :frowning:

[editline]22nd January 2012[/editline]

I just noticed i can’t create the actually file when doing file.CreateDir().

Can i do file.Write(“wep_save/…ply:UniqueID()…/THEN CREATE THE FILE HER ???”)

You can’t make a file with file.CreateDir. You have to use file.Write instead. The directory will be made if it doesn’t exist already.

ok, also file.Write(“wep_save/…ply:UniqueID()…/weapon.txt”,“CONTENT TO SAVE”)

???

Yes.

when you buy a weapon, a console command liek this is being called

RunConsoleCommand(“save_wep”)

and the function that is being called is:

function wep_sav(ply, weapon)

if file.Exists("/wep_save/"…ply:UniqueID()…"/weapon.txt") then
return false
else
local ply = weapon:GetOwner()
file.CreateDir("/wep_save/"…ply:UniqueID())
file.Write("/wep_save/"…ply:UniqueID()…"/weapon.txt",weapon:GetClass())
end
end

concommand.Add(“save_wep”,wep_sav)

BUT!! I do get this error:

[gamemodes\darkrp\gamemode\sv_gamemode_functions.lua:585] attempt to call method
‘UniqueID’ (a nil value)

???

[editline]23rd January 2012[/editline]

when you buy a weapon, a console command liek this is being called

RunConsoleCommand(“save_wep”)

and the function that is being called is:

function wep_sav(ply, weapon)
if (SERVER) then
if file.Exists("/wep_save/"…ply:UniqueID()…"/weapon.txt") then
return
else
local ply = weapon:GetOwner()
file.CreateDir("/wep_save/"…ply:UniqueID())
file.Write("/wep_save/"…ply:UniqueID()…"/weapon.txt",weapon:GetClass())
end
end
end

concommand.Add(“save_wep”,wep_sav)

BUT!! I do get this error:

[gamemodes\darkrp\gamemode\sv_gamemode_functions.lua:585] attempt to call method
‘UniqueID’ (a nil value)

???

-snip-

huh…?

[lua]
concommand.Add(“save_wep”, function(client, command, arguments)
local wep = client:GetActiveWeapon();

 if ( ValidEntiy(wep) ) thrn
      local class = wep:GetClass();

      file.Write("wep_save/"..client:UniqueID().."/weapon.txt", class);
 end;

end);
[/lua]

i love you man, thanks… i learned something rosat :F

[editline]24th January 2012[/editline]

Also, please use lua tags next time.

Ok, im still getting an error, and its this

[gamemodes\darkrp\gamemode\sv_gamemode_functions.lua:586] attempt to call method
‘GetActiveWeapon’ (a nil value)

With this Lua script



concommand.Add("save_wep", function(client, command, arguments)
     local wep = client:GetActiveWeapon();
 
     if ( ValidEntiy(wep) ) then
          local class = wep:GetClass();
 
          file.Write("wep_save/"..client:UniqueID().."/weapon.txt", class);
     end;
end);


What is client?

client is the same as ply, pl, and what not.