Jump to content

[Question] Saving weapons using getPedWeapons


kieran

Recommended Posts

Posted

So I have finally realized what good are all these scripts if I don't have weapons or money etc and easiest way to get them is login, plus it means I can get it in map editor! :D But I have hit a "brick wall" and for the life of me I can't figure out how to save player weapons, here's what I'm trying...

 

function MoneyWepSave ( )
	local playeraccount = getPlayerAccount ( source )
	if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
	local playermoney = getPlayerMoney ( source )
    setAccountData ( playeraccount, "test.money", playermoney ) 
		local for i,wep in ipairs(getPedWeapons(player)) do
		setAccountData ( playeraccount, "test.weps", playerweps )
		end
	end
end
 
function MoneyWepGive (_, playeraccount )
      if ( playeraccount ) then
            local playermoney = getAccountData ( playeraccount, "test.money" )
			local playerweps = getAccountData ( playeraccount, "test.weps" )
            if ( playermoney ) then
                  setPlayerMoney ( source, playermoney )
			if ( playermoney ) then
				  givePedWeaapons ( source, playerweps )
            end
			end
      end
end

function MoneyWepReset (_, playeraccount )
	--How would I set weps to 0?
	setPlayerMoney ( source, 0 )
end
addEventHandler ( "onPlayerQuit", getRootElement ( ), MoneyWepSave )
addEventHandler ( "onPlayerLogin", getRootElement ( ), MoneyWepGive )
addEventHandler ( "onPlayerLogout", getRootElement ( ), MoneyWepReset )

I could just get them for each individual slot, but I thought this way would be easier...  Any help is appreciated!  Thanks in advance :D 

Posted (edited)
function getPedWeapons(ped)
	local playerWeapons = {}
	if ped and isElement(ped) and getElementType(ped) == "ped" or getElementType(ped) == "player" then
		for i=2,9 do
			local wep = getPedWeapon(ped,i)
			if wep and wep ~= 0 then
				table.insert(playerWeapons,wep)
			end
		end
	else
		return false
	end
	return playerWeapons
end

function saveData()
	local account = getPlayerAccount(source)
	if not account or isGuestAccount(account) then return end
	local playermoney = getPlayerMoney(source)
	setAccountData(account, "test.money", playermoney)
	setAccountData(playeraccount, "test.weps", getPedWeapons(source))
end
 
function loadData(_, account)
	if account then
		local playermoney = getAccountData(account, "test.money")
		local playerweps = getAccountData(account, "test.weps")
		if playermoney then
			setPlayerMoney (source, playermoney)
		end
		if playerweps then
			for i,v in pairs(playerweps) do
				giveWeapon(source, v)
			end
		end
	end
end

function resetData(account)
	local playermoney = getPlayerMoney(source)
	setAccountData(account, "test.money", playermoney)
	setAccountData(playeraccount, "test.weps", getPedWeapons(source))
	takeAllWeapons(source)
	setPlayerMoney(source, 0)
end
addEventHandler ("onPlayerQuit", getRootElement (), saveData)
addEventHandler ("onPlayerLogin", getRootElement (), loadData)
addEventHandler ("onPlayerLogout", getRootElement (), resetData)

Well I'm not sure that it will work but it's worth a try 

Edited by kikos500
Posted (edited)

Nah, keeps saying it expects account at argument 1, got nil.....  Weird :/ (line 74 and 76)

Edited by kieran
Posted
11 minutes ago, DNL291 said:

The data stored with setAccountData must be a string. Use toJSON to store weapons and fromJSON when you get the value.

So I could just use getPlayerWeapon and leave ammo empty?  Sorry, I am a bit of a newbie... :P 

Posted

No, I mean, make a table and store each weapon and the current ammo. After that, use toJSON to convert that table to string so you can store it using setAccountData.

Like this:

local weapons = {}
for i=1,12 do
	weapons[i] = { getPedWeapon( player, i ), getPedTotalAmmo( player, i ) }
end
setAccountData( acc, "test.weps", toJSON( weapons ) )

For the money, just use tostring.

Posted
On 7/3/2017 at 04:46, DNL291 said:

The data stored with setAccountData must be a string. Use toJSON to store weapons and fromJSON when you get the value.

didn't know about that never did much with account data anyways :D

Posted

@DNL291 I'm pretty sure that it can save numbers as well. I'm using account data to save coins and points at my server and don't have to use "tostring" every time i save the new value (it's a number).

Posted
8 minutes ago, koragg said:

@DNL291 I'm pretty sure that it can save numbers as well. I'm using account data to save coins and points at my server and don't have to use "tostring" every time i save the new value (it's a number).

Yes, setAccountData also saves numbers. I didn't know that because in MTA Wiki it says that the value to be stored must be a string:

Quote

 

Syntax


bool setAccountData ( account theAccount, string key, string value )

 

Just did a test and indeed it stores numbers as well:

Quote

run setAccountData(getAccount("Console"), "test", 12345)
[02:42:25] Console executed command: setAccountData(getAccount("Console"), "test
", 12345)
[02:42:25] Command results: true [boolean]
run getAccountData(getAccount("Console"), "test")
[02:42:52] Console executed command: getAccountData(getAccount("Console"), "test
")
[02:42:52] Command results: 12345 [number]
run type(getAccountData(getAccount("Console"), "test"))
[02:43:24] Console executed command: type(getAccountData(getAccount("Console"),
"test"))
[02:43:24] Command results: number [string]

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...