Jump to content

Weapon Saving Problem


DonOmar

Recommended Posts

Hello there i used Account and Element data to make a weapon save system and works fine , but if someone dead and disconnected from the server His weapons will be lost. Here is the code : 

function onPlayerQuitWeaponSave()

    local playeraccount = getPlayerAccount(source)

    if (playeraccount) and not isGuestAccount(playeraccount) then

        if getElementData(source,"22") == false then

            local ca = 3

            setAccountData(playeraccount, "cadd", ca)

        else

            if getElementData(source,"22") == true then

                local caa = 2

                setAccountData(playeraccount, "cadd", caa)

            end

        end

        if getElementData(source,"24") == false then

            local da = 3

            setAccountData ( playeraccount, "dadd", da )

        else

            if getElementData(source,"24") == true then

                local daa = 2

                setAccountData ( playeraccount, "dadd", daa )

            end

        end

        if getElementData(source,"23") == false then

            local sila = 3

            setAccountData ( playeraccount, "siladd", sila )

        else

            if getElementData(source,"23") == true then

                local silaa = 2

                setAccountData ( playeraccount, "siladd", silaa )

            end

        end

        if getElementData(source,"25") == false then

            local sha = 3

            setAccountData ( playeraccount, "shadd", sha )

        else

            if getElementData(source,"25") == true then

                local shaa = 2

                setAccountData ( playeraccount, "shadd", shaa )

            end

        end

      if getElementData(source,"26") == false then

            local swa = 3

            setAccountData ( playeraccount, "swadd", swa )

else

      if getElementData(source,"26") == true then

            local swaa = 2

            setAccountData ( playeraccount, "swadd", swaa )

        end

    end

      if getElementData(source,"27") == false then

            local spa = 3

            setAccountData ( playeraccount, "spadd", spa )

else

      if getElementData(source,"27") == true then

            local spaa = 2

            setAccountData ( playeraccount, "spadd", spaa )

        end

    end

      if getElementData(source,"30") == false then

            local aka = 3

            setAccountData ( playeraccount, "akadd", aka )

else

      if getElementData(source,"30") == true then

            local akaa = 2

            setAccountData ( playeraccount, "akadd", akaa )

        end

    end

      if getElementData(source,"31") == false then

            local ma = 3

            setAccountData ( playeraccount, "madd", ma )

else

      if getElementData(source,"31") == true then

            local maa = 2

            setAccountData ( playeraccount, "madd", maa )

        end

    end

      if getElementData(source,"28") == false then

            local ua = 3

            setAccountData ( playeraccount, "uadd", ua )

else

      if getElementData(source,"28") == true then

            local uaa = 2

            setAccountData ( playeraccount, "uadd", uaa )

        end

    end

      if getElementData(source,"32") == false then

            local ta = 3

            setAccountData ( playeraccount, "tadd", ta )

else

      if getElementData(source,"32") == true then

            local taa = 2

            setAccountData ( playeraccount, "tadd", taa )

        end

    end

      if getElementData(source,"29") == false then

            local mpa = 3

            setAccountData ( playeraccount, "mpadd", mpa )

else

      if getElementData(source,"29") == true then

            local mpaa = 2
            setAccountData ( playeraccount, "mpadd", mpaa )

        end

    end

      if getElementData(source,"33") == false then

            local ra = 3

            setAccountData ( playeraccount, "radd", ra )

else

      if getElementData(source,"33") == true then

            local raa = 2

            setAccountData ( playeraccount, "radd", raa )

        end

    end

      if getElementData(source,"34") == false then

            local sna = 3

            setAccountData ( playeraccount, "snadd", sna )

else

      if getElementData(source,"34") == true then

            local snaa = 2

            setAccountData ( playeraccount, "snadd", snaa )

        end

    end

      if getElementData(source,"38") == false then

            local mia = 3

            setAccountData ( playeraccount, "miadd", mia )

else

      if getElementData(source,"38") == true then

            local miaa = 2

            setAccountData ( playeraccount, "miadd", miaa )

        end

    end

    end

end

addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuitWeaponSave)

 

function onPlayerLoginWeaponSave(_, playeraccount)

      if ( playeraccount ) then

            local ca = getAccountData ( playeraccount, "cadd" )

            if (ca == 2 ) then

                  setElementData (source, "22", true)

            end

            local da = getAccountData ( playeraccount, "dadd" )

            if (da == 2 ) then

                  setElementData (source, "24", true)

            end

            local sila = getAccountData ( playeraccount, "siladd" )

            if (sila == 2 ) then

                  setElementData (source, "23", true)

            end

            local sha = getAccountData ( playeraccount, "shadd" )

            if (sha == 2 ) then

                  setElementData (source, "25", true)

            end

            local swa = getAccountData ( playeraccount, "swadd" )

            if (swa == 2 ) then

                  setElementData (source, "26", true)

            end

            local spa = getAccountData ( playeraccount, "spadd" )

            if (spa == 2 ) then

                  setElementData (source, "27", true)

            end

            local aka = getAccountData ( playeraccount, "akadd" )

            if (aka == 2 ) then

                  setElementData (source, "30", true)

            end

            local ma = getAccountData ( playeraccount, "madd" )

            if (ma == 2 ) then

                  setElementData (source, "31", true)

            end

            local ua = getAccountData ( playeraccount, "uadd" )

            if (ua == 2 ) then

                  setElementData (source, "28", true)

            end

            local ta = getAccountData ( playeraccount, "tadd" )

            if (ta == 2 ) then

                  setElementData (source, "32", true)

            end

            local mpa = getAccountData ( playeraccount, "mpadd" )

            if (mpa == 2 ) then

                  setElementData (source, "29", true)

            end

            local ra = getAccountData ( playeraccount, "radd" )

            if (ra == 2 ) then

                  setElementData (source, "33", true)

            end

            local sna = getAccountData ( playeraccount, "snadd" )

            if (sna == 2 ) then

                  setElementData (source, "34", true)

            end

            local mia = getAccountData ( playeraccount, "miadd" )

            if (mia == 2 ) then

                  setElementData (source, "38", true)

            end

      end

end

addEventHandler("onPlayerLogin", getRootElement(), onPlayerLoginWeaponSave)

 

i tried to use addEventHandler("onPlayerWasted", getRootElement(), onPlayerQuitWeaponSave)  but it didnt work with me .

Edited by DonOmar
Link to comment
Just now, DonOmar said:

it doesn't work. also i wanna save fix my save system.

 

function onPlayerQuitWeaponSave()
    local playeraccount = getPlayerAccount(source)
    if (playeraccount) and not isGuestAccount(playeraccount) then
        if getElementData(source,"22") == false then
            local ca = 3
            setAccountData(playeraccount, "cadd", ca)
        else
            if getElementData(source,"22") == true then
                local caa = 2
                setAccountData(playeraccount, "cadd", caa)
            end
        end
        if getElementData(source,"24") == false then
            local da = 3
            setAccountData ( playeraccount, "dadd", da )
        else
            if getElementData(source,"24") == true then
                local daa = 2
                setAccountData ( playeraccount, "dadd", daa )
            end
        end
        if getElementData(source,"23") == false then
            local sila = 3
            setAccountData ( playeraccount, "siladd", sila )
        else
            if getElementData(source,"23") == true then
                local silaa = 2
                setAccountData ( playeraccount, "siladd", silaa )
            end
        end
        if getElementData(source,"25") == false then
            local sha = 3
            setAccountData ( playeraccount, "shadd", sha )
        else
            if getElementData(source,"25") == true then
                local shaa = 2
                setAccountData ( playeraccount, "shadd", shaa )
            end
        end
      if getElementData(source,"26") == false then
            local swa = 3
            setAccountData ( playeraccount, "swadd", swa )
else
      if getElementData(source,"26") == true then
            local swaa = 2
            setAccountData ( playeraccount, "swadd", swaa )
        end
    end
      if getElementData(source,"27") == false then
            local spa = 3
            setAccountData ( playeraccount, "spadd", spa )
else
      if getElementData(source,"27") == true then
            local spaa = 2
            setAccountData ( playeraccount, "spadd", spaa )
        end
    end
      if getElementData(source,"30") == false then
            local aka = 3
            setAccountData ( playeraccount, "akadd", aka )
else
      if getElementData(source,"30") == true then
            local akaa = 2
            setAccountData ( playeraccount, "akadd", akaa )
        end
    end
      if getElementData(source,"31") == false then
            local ma = 3
            setAccountData ( playeraccount, "madd", ma )
else
      if getElementData(source,"31") == true then
            local maa = 2
            setAccountData ( playeraccount, "madd", maa )
        end
    end
      if getElementData(source,"28") == false then
            local ua = 3
            setAccountData ( playeraccount, "uadd", ua )
else
      if getElementData(source,"28") == true then
            local uaa = 2
            setAccountData ( playeraccount, "uadd", uaa )
        end
    end
      if getElementData(source,"32") == false then
            local ta = 3
            setAccountData ( playeraccount, "tadd", ta )
else
      if getElementData(source,"32") == true then
            local taa = 2
            setAccountData ( playeraccount, "tadd", taa )
        end
    end
      if getElementData(source,"29") == false then
            local mpa = 3
            setAccountData ( playeraccount, "mpadd", mpa )
else
      if getElementData(source,"29") == true then
            local mpaa = 2
            setAccountData ( playeraccount, "mpadd", mpaa )
        end
    end
      if getElementData(source,"33") == false then
            local ra = 3
            setAccountData ( playeraccount, "radd", ra )
else
      if getElementData(source,"33") == true then
            local raa = 2
            setAccountData ( playeraccount, "radd", raa )
        end
    end
      if getElementData(source,"34") == false then
            local sna = 3
            setAccountData ( playeraccount, "snadd", sna )
else
      if getElementData(source,"34") == true then
            local snaa = 2
            setAccountData ( playeraccount, "snadd", snaa )
        end
    end
      if getElementData(source,"38") == false then
            local mia = 3
            setAccountData ( playeraccount, "miadd", mia )
else
      if getElementData(source,"38") == true then
            local miaa = 2
            setAccountData ( playeraccount, "miadd", miaa )
        end
    end
    end
end
addEventHandler("onPlayerQuit", root, onPlayerQuitWeaponSave)
addEventHandler("onPlayerWasted", root, onPlayerQuitWeaponSave)

function onPlayerLoginWeaponSave(_, playeraccount)
      if ( playeraccount ) then
            local ca = getAccountData ( playeraccount, "cadd" )
            if (ca == 2 ) then
                  setElementData (source, "22", true)
            end
            local da = getAccountData ( playeraccount, "dadd" )
            if (da == 2 ) then
                  setElementData (source, "24", true)
            end
            local sila = getAccountData ( playeraccount, "siladd" )
            if (sila == 2 ) then
                  setElementData (source, "23", true)
            end
            local sha = getAccountData ( playeraccount, "shadd" )
            if (sha == 2 ) then
                  setElementData (source, "25", true)
            end
            local swa = getAccountData ( playeraccount, "swadd" )
            if (swa == 2 ) then
                  setElementData (source, "26", true)
            end
            local spa = getAccountData ( playeraccount, "spadd" )
            if (spa == 2 ) then
                  setElementData (source, "27", true)
            end
            local aka = getAccountData ( playeraccount, "akadd" )
            if (aka == 2 ) then
                  setElementData (source, "30", true)
            end
            local ma = getAccountData ( playeraccount, "madd" )
            if (ma == 2 ) then
                  setElementData (source, "31", true)
            end
            local ua = getAccountData ( playeraccount, "uadd" )
            if (ua == 2 ) then
                  setElementData (source, "28", true)
            end
            local ta = getAccountData ( playeraccount, "tadd" )
            if (ta == 2 ) then
                  setElementData (source, "32", true)
            end
            local mpa = getAccountData ( playeraccount, "mpadd" )
            if (mpa == 2 ) then
                  setElementData (source, "29", true)
            end
            local ra = getAccountData ( playeraccount, "radd" )
            if (ra == 2 ) then
                  setElementData (source, "33", true)
            end
            local sna = getAccountData ( playeraccount, "snadd" )
            if (sna == 2 ) then
                  setElementData (source, "34", true)
            end
            local mia = getAccountData ( playeraccount, "miadd" )
            if (mia == 2 ) then
                  setElementData (source, "38", true)
            end
      end
end

addEventHandler("onPlayerLogin", root, onPlayerLoginWeaponSave)
addEventHandler("OnPlayerSpawn", root, onPlayerLoginWeaponSave)

 

Link to comment
  • Moderators

Try this:


function saveWeapons( p, toAcc )
	local weapons = {}
	for i=1,12 do
		weapons[i] = { getPedWeapon( p, i ), getPedTotalAmmo( p, i ) }
	end
	if toAcc then return setAccountData( getPlayerAccount(p), "playerWeapons", toJSON( weapons ) ) end
	
	setElementData( p, "playerWeapons", toJSON( weapons ), false )
end

function loadWeapons( p, fromAcc )
	if fromAcc then
		local weps = getAccountData( getPlayerAccount(p), "playerWeapons" )
		if weps then return setElementData( p, "playerWeapons", weps, false ) end
	else
		local weps = getElementData( p, "playerWeapons" )
		if weps then for weapon, ammo in ipairs( fromJSON(weps) ) do giveWeapon( p, weapon, ammo, true ) end end
	end
end

addEventHandler( "onPlayerWasted", root, function() saveWeapons( source ) end )
addEventHandler( "onPlayerQuit", root, function() saveWeapons( source, true ) end )
addEventHandler( "onPlayerLogin", root, function() loadWeapons( source, true ) end )
addEventHandler( "onPlayerSpawn", root, function() loadWeapons( source ) end )

Edit:

6 hours ago, DonOmar said:

and works fine , but if someone dead and disconnected from the server His weapons will be lost.

This is kind of controversial, actually nothing is working then lol.

Edited by DNL291
Link to comment
  • Moderators
On 21/07/2017 at 18:24, DonOmar said:

@DNL291 Didn't Work.

It had an error at line 18, I looped the table the wrong way.
I tested my code and now it works, try this:


function saveWeapons( p, toAcc )
	local weapons = {}
	for i=1,12 do
		weapons[i] = { getPedWeapon( p, i ), getPedTotalAmmo( p, i ) }
	end
	if toAcc then return setAccountData( getPlayerAccount(p), "playerWeapons", toJSON( weapons ) ) end
	
	setElementData( p, "playerWeapons", toJSON( weapons ), false )
end

function loadWeapons( p, fromAcc )
	if fromAcc then
		local weps = getAccountData( getPlayerAccount(p), "playerWeapons" )
		if weps then return setElementData( p, "playerWeapons", weps, false ) end
	else
		local weps = getElementData( p, "playerWeapons" )
		if weps then for i, v in ipairs( fromJSON(weps) ) do giveWeapon( p, v[1], v[2], true ) end end
	end
end
addEventHandler( "onPlayerWasted", root, function() saveWeapons( source ) end )
addEventHandler( "onPlayerQuit", root, function() saveWeapons( source, true ) end )
addEventHandler( "onPlayerLogin", root, function() loadWeapons( source, true ) end )
addEventHandler( "onPlayerSpawn", root, function() loadWeapons( source ) end )

Always remember to use /debugscript 3 when testing any script.

Edited by DNL291
Link to comment
11 hours ago, DNL291 said:

It had an error at line 18, I looped the table the wrong way.
I tested my code and now it works, try this:


function saveWeapons( p, toAcc )	local weapons = {}	for i=1,12 do		weapons[i] = { getPedWeapon( p, i ), getPedTotalAmmo( p, i ) }	end	if toAcc then return setAccountData( getPlayerAccount(p), "playerWeapons", toJSON( weapons ) ) end		setElementData( p, "playerWeapons", toJSON( weapons ), false )endfunction loadWeapons( p, fromAcc )	if fromAcc then		local weps = getAccountData( getPlayerAccount(p), "playerWeapons" )		if weps then return setElementData( p, "playerWeapons", weps, false ) end	else		local weps = getElementData( p, "playerWeapons" )		if weps then for i, v in ipairs( fromJSON(weps) ) do giveWeapon( p, v[1], v[2], true ) end end	endendaddEventHandler( "onPlayerWasted", root, function() saveWeapons( source ) end )addEventHandler( "onPlayerQuit", root, function() saveWeapons( source, true ) end )addEventHandler( "onPlayerLogin", root, function() loadWeapons( source, true ) end )addEventHandler( "onPlayerSpawn", root, function() loadWeapons( source ) end )

Always remember to use /debugscript 3 when testing any script.

When i reconnected while dead i lost my weapons :/ .

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