Jump to content

Saved weapons don't load correctly from database


Recommended Posts

-- This piece of code in located in the 'onPlayerQuit' event handler. It saves the player's weapons and ammo if he has any, this is all working correctly
local weapons = {}
		
for slot = 1, 12 do
	local weapon = getPedWeapon(source, slot)
			
	if weapon > 0 then
		local ammo = getPedTotalAmmo(source, slot)
				
		if ammo > 0 then
			table.insert(weapons, {weapon, ammo})
		end
	end
end

-- This is what appears under the 'weapons' column in the player's account table entry
[ [ [ 30, 176 ] ] ] -- Which means the player left the server and had an AK-47 with total 176 ammo left

-- And this is the part which loads the SQL weapons data
for weapon, ammo in ipairs(fromJSON(accountData.weapons)) do
	giveWeapon(player, tonumber(weapon), tonumber(ammo))
end
-- The problem is, the player doesn't get the weapons he has saved, instead, he'll be given a brassknuckle everytime and the SQL entry is reset

 

Any ideas why it is not working properly? I'm using SQLite not MySQL. The weapons are saved correctly, so it would seem that the problems stems in the loading part.

Link to comment

This is how i save and load:

--save
function getPlayerWeapons( p )
    local weps={}
    for i=1,12 do
        weps[i]={getPedWeapon( p, i ),getPedTotalAmmo( p, i )}
    end
    return toJSON(weps)
end

--load
local weaponTable = fromJSON(d.weapons)
for i=1,12 do
    local row = weaponTable[i]
    if tonumber(row[2]) > 0 then
        giveWeapon( plr, row[1], row[2] )
    end
end

 

  • Thanks 1
Link to comment

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...