Jump to content

Flamethrower on save weapon doubling?


Recommended Posts

I have a script that saves weapon on death / reconnect, but the flamethrower ammo doubles! why?

It works perfectly on all types of weapons, its just the flamethrower, is it because it's ammo is different? if so how do I fix it?

Image of before & after:

https://imgur.com/a/v2c1iXF

local playerWeapons = { }

addEventHandler ( "onPlayerWasted", root,
	function ( )
		if ( not playerWeapons [ source ] ) then
			playerWeapons [ source ] = { }
		end
		for slot = 0, 12 do
			local weapon = getPedWeapon ( source, slot )
			if ( weapon > 0 ) then
				local ammo = getPedTotalAmmo ( source, slot )
				if ( ammo > 0 ) then
					playerWeapons [ source ] [ weapon ] = ammo
				end
			end
		end
	end
)

addEventHandler ( "onPlayerSpawn", root,
	function ( )
		if ( playerWeapons [ source ] ) then
			for weapon, ammo in pairs ( playerWeapons [ source ] ) do
				giveWeapon ( source, tonumber ( weapon ), tonumber ( ammo ) )
			end
		end
		playerWeapons [ source ] = nil
	end
)

 

Edited by Shaman123
Link to post
5 minutes ago, Shaman123 said:

so there's no fix to it? damn

Is it multiplying it by a fixed value or it's different everytime ?
In the issues they say that it muplties it by 10. So if the actual value is 2 it will return 20. If it's like this you can save a value divided by 10.
I could test it later to see how it behaves.

Edited by SpecT
Link to post
2 minutes ago, SpecT said:

Is it multiplying it by a fixed value or it's different everytime ?
In the issues they say that it muplties it by 10. So if the actual value is 2 it will return 20. If it's like this you can save a value divided by 10.
I could test it later to see how it behaves.

its fixed number, and btw thanks I would appreciate any help.

Link to post

This should solve the problem:

local playerWeapons = { }

addEventHandler ( "onPlayerWasted", root,
	function ( )
		if ( not playerWeapons [ source ] ) then
			playerWeapons [ source ] = { }
		end
		for slot = 0, 12 do
			local weapon = getPedWeapon ( source, slot )
			if ( weapon > 0 ) then
				local ammo = getPedTotalAmmo ( source, slot )
				if ( ammo > 0 ) then
					if (weapon == 37) then -- flame thrower
						ammo = ammo/10
					end
					playerWeapons [ source ] [ weapon ] = ammo
				end
			end
		end
	end
)

addEventHandler ( "onPlayerSpawn", root,
	function ( )
		if ( playerWeapons [ source ] ) then
			for weapon, ammo in pairs ( playerWeapons [ source ] ) do
				giveWeapon ( source, tonumber ( weapon ), tonumber ( ammo ) )
			end
		end
		playerWeapons [ source ] = nil
	end
)

 

Link to post
5 hours ago, SpecT said:

This should solve the problem:


local playerWeapons = { }

addEventHandler ( "onPlayerWasted", root,
	function ( )
		if ( not playerWeapons [ source ] ) then
			playerWeapons [ source ] = { }
		end
		for slot = 0, 12 do
			local weapon = getPedWeapon ( source, slot )
			if ( weapon > 0 ) then
				local ammo = getPedTotalAmmo ( source, slot )
				if ( ammo > 0 ) then
					if (weapon == 37) then -- flame thrower
						ammo = ammo/10
					end
					playerWeapons [ source ] [ weapon ] = ammo
				end
			end
		end
	end
)

addEventHandler ( "onPlayerSpawn", root,
	function ( )
		if ( playerWeapons [ source ] ) then
			for weapon, ammo in pairs ( playerWeapons [ source ] ) do
				giveWeapon ( source, tonumber ( weapon ), tonumber ( ammo ) )
			end
		end
		playerWeapons [ source ] = nil
	end
)

 

it works perfectly, thank you but how can i apply it on my save accountdata stuff? cause' the format is kinda different, sarreh im kinda confused

onSpawn=

function playerLogin (thePreviousAccount, theCurrentAccount, autoLogin) 
  if  not (isGuestAccount (getPlayerAccount (source))) then 
    local accountData = getAccountData (theCurrentAccount, "money") 
    if (accountData) then 
      local playerWeaponID12 = getAccountData (theCurrentAccount, "weaponID7") 
      local playerWeaponAmmo12 = getAccountData (theCurrentAccount, "weaponAmmo7") 
      spawnPlayer (source, playerX, playerY, playerZ +1, 0, playerSkin, playerInt, playerDim) 
      giveWeapon(source, playerWeaponID7, playerWeaponAmmo7, false) 
      setCameraTarget (source, source) 
      fadeCamera(source, true, 2.0) 
    else 
      fadeCamera(source, true, 2.0)   
    end   
  end 
end 
addEventHandler ("onPlayerLogin", getRootElement(), playerLogin) 

function onQuit (quitType, reason, responsibleElement) 
    if not (isGuestAccount (getPlayerAccount (source))) then 
        local account = getPlayerAccount (source) 
        if (account) then 
      setAccountData (account, "weaponID7", getPedWeapon (source, 7)) 
      setAccountData (account, "weaponAmmo7", getPedTotalAmmo (source, 7)) 
  end
end 
end
addEventHandler ("onPlayerQuit", getRootElement(), onQuit) 

 

 

Link to post

all of the weapons, the original code is not just for weapons and it's messy and not so optimized, so behind that code has all sorts of getIDs and element datas lol, I just need the weapon save optimized, since that flamethrower save is buggy. but anyway here's the full one:

function playerLogin (thePreviousAccount, theCurrentAccount, autoLogin) 
  if  not (isGuestAccount (getPlayerAccount (source))) then 
    local accountData = getAccountData (theCurrentAccount, "money") 
    if (accountData) then 
      local oldplayer = getAccountData (theCurrentAccount, "oldplayer")
      local playerMoney = getAccountData (theCurrentAccount, "money") 
      local playerSkin = getAccountData (theCurrentAccount, "skin") 
      local playerHealth = getAccountData (theCurrentAccount, "health") 
      local playerArmor = getAccountData (theCurrentAccount, "armor") 
      local playerX = getAccountData (theCurrentAccount, "x") 
      local playerY = getAccountData (theCurrentAccount, "y") 
      local playerZ = getAccountData (theCurrentAccount, "z") 
      local playerInt = getAccountData (theCurrentAccount, "int") 
      local playerDim = getAccountData (theCurrentAccount, "dim") 
     local playerTeam = getAccountData (theCurrentAccount, "Team") 
      local playerWeaponID0 = getAccountData (theCurrentAccount, "weaponID0") 
      local playerWeaponID1 = getAccountData (theCurrentAccount, "weaponID1") 
      local playerWeaponID2 = getAccountData (theCurrentAccount, "weaponID2") 
      local playerWeaponID3 = getAccountData (theCurrentAccount, "weaponID3") 
      local playerWeaponID4 = getAccountData (theCurrentAccount, "weaponID4") 
      local playerWeaponID5 = getAccountData (theCurrentAccount, "weaponID5") 
      local playerWeaponID6 = getAccountData (theCurrentAccount, "weaponID6") 
      local playerWeaponID7 = getAccountData (theCurrentAccount, "weaponID7") 
      local playerWeaponID8 = getAccountData (theCurrentAccount, "weaponID8") 
      local playerWeaponID9 = getAccountData (theCurrentAccount, "weaponID9") 
      local playerWeaponID10 = getAccountData (theCurrentAccount, "weaponID10") 
      local playerWeaponID11 = getAccountData (theCurrentAccount, "weaponID11") 
      local playerWeaponID12 = getAccountData (theCurrentAccount, "weaponID12") 
      local playerWeaponAmmo0 = getAccountData (theCurrentAccount, "weaponAmmo0") 
      local playerWeaponAmmo1 = getAccountData (theCurrentAccount, "weaponAmmo1") 
      local playerWeaponAmmo2 = getAccountData (theCurrentAccount, "weaponAmmo2") 
      local playerWeaponAmmo3 = getAccountData (theCurrentAccount, "weaponAmmo3") 
      local playerWeaponAmmo4 = getAccountData (theCurrentAccount, "weaponAmmo4") 
      local playerWeaponAmmo5 = getAccountData (theCurrentAccount, "weaponAmmo5") 
      local playerWeaponAmmo6 = getAccountData (theCurrentAccount, "weaponAmmo6") 
      local playerWeaponAmmo7 = getAccountData (theCurrentAccount, "weaponAmmo7") 
      local playerWeaponAmmo8 = getAccountData (theCurrentAccount, "weaponAmmo8") 
      local playerWeaponAmmo9 = getAccountData (theCurrentAccount, "weaponAmmo9") 
      local playerWeaponAmmo10 = getAccountData (theCurrentAccount, "weaponAmmo10") 
      local playerWeaponAmmo11 = getAccountData (theCurrentAccount, "weaponAmmo11") 
      local playerWeaponAmmo12 = getAccountData (theCurrentAccount, "weaponAmmo12") 
      setElementData(source, "selecting", nil)
      local role = getAccountData ( theCurrentAccount, "Class" )
       			setElementData ( source, "Class", role )
 local occupation = getAccountData ( theCurrentAccount, "Occupation" )
       			setElementData ( source, "Occupation", occupation )
      spawnPlayer (source, playerX, playerY, playerZ +1, 0, playerSkin, playerInt, playerDim) 
      setPlayerMoney (source, playerMoney) 
      setTimer (setElementHealth, 500, 1, source, playerHealth) 
      setTimer (setPedArmor, 500, 1, source, playerArmor) 
      setTimer (setPlayerWantedLevel, 500, 1, source, playerWanted) 
      giveWeapon(source, playerWeaponID0, playerWeaponAmmo0, true) 
      giveWeapon(source, playerWeaponID1, playerWeaponAmmo1, false) 
      giveWeapon(source, playerWeaponID2, playerWeaponAmmo2, false) 
      giveWeapon(source, playerWeaponID3, playerWeaponAmmo3, false) 
      giveWeapon(source, playerWeaponID4, playerWeaponAmmo4, false) 
      giveWeapon(source, playerWeaponID5, playerWeaponAmmo5, false) 
      giveWeapon(source, playerWeaponID6, playerWeaponAmmo6, false) 
      giveWeapon(source, playerWeaponID7, playerWeaponAmmo7, false) 
      giveWeapon(source, playerWeaponID8, playerWeaponAmmo8, false) 
      giveWeapon(source, playerWeaponID9, playerWeaponAmmo9, false) 
      giveWeapon(source, playerWeaponID10, playerWeaponAmmo10, false) 
      giveWeapon(source, playerWeaponID11, playerWeaponAmmo11, false) 
      giveWeapon(source, playerWeaponID12, playerWeaponAmmo12, false) 
      setCameraTarget (source, source) 
      fadeCamera(source, true, 2.0) 
    else 
      fadeCamera(source, true, 2.0)   
    end   
  end 
end 
addEventHandler ("onPlayerLogin", getRootElement(), playerLogin) 

function onQuit (quitType, reason, responsibleElement) 
    if not (isGuestAccount (getPlayerAccount (source))) then 
        local account = getPlayerAccount (source) 
        if (account) then 
local role = tostring (getElementData ( source, "Class" ))
	setAccountData ( account, "Class", role )
local occupation = tostring (getElementData ( source, "Occupation" ))
	setAccountData ( account, "Occupation", occupation )
            local x,y,z = getElementPosition (source) 
            setAccountData (account, "money", tostring (getPlayerMoney (source))) 
            setAccountData (account, "skin", tostring (getPedSkin (source))) 
            setAccountData (account, "health", tostring (getElementHealth (source))) 
            setAccountData (account, "armor", tostring (getPedArmor (source))) 
            setAccountData (account, "oldplayer", "yes")
 setAccountData (account, "x", x) 
            setAccountData (account, "y", y) 
            setAccountData (account, "z", z) 
            setAccountData (account, "int", getElementInterior (source)) 
            setAccountData (account, "dim", getElementDimension (source)) 
            setAccountData (account, "weaponID0", getPedWeapon (source, 0)) 
            setAccountData (account, "weaponID1", getPedWeapon (source, 1)) 
      setAccountData (account, "weaponID2", getPedWeapon (source, 2)) 
      setAccountData (account, "weaponID3", getPedWeapon (source, 3)) 
      setAccountData (account, "weaponID4", getPedWeapon (source, 4)) 
      setAccountData (account, "weaponID5", getPedWeapon (source, 5)) 
      setAccountData (account, "weaponID6", getPedWeapon (source, 6)) 
      setAccountData (account, "weaponID7", getPedWeapon (source, 7)) 
      setAccountData (account, "weaponID8", getPedWeapon (source, 8))
      setAccountData (account, "weaponID9", getPedWeapon (source, 9)) 
      setAccountData (account, "weaponID10", getPedWeapon (source, 10)) 
      setAccountData (account, "weaponID11", getPedWeapon (source, 11)) 
      setAccountData (account, "weaponID12", getPedWeapon (source, 12)) 
      setAccountData (account, "weaponAmmo0", getPedTotalAmmo (source, 0)) 
      setAccountData (account, "weaponAmmo1", getPedTotalAmmo (source, 1)) 
      setAccountData (account, "weaponAmmo2", getPedTotalAmmo (source, 2)) 
      setAccountData (account, "weaponAmmo3", getPedTotalAmmo (source, 3)) 
      setAccountData (account, "weaponAmmo4", getPedTotalAmmo (source, 4)) 
      setAccountData (account, "weaponAmmo5", getPedTotalAmmo (source, 5)) 
      setAccountData (account, "weaponAmmo6", getPedTotalAmmo (source, 6)) 
      setAccountData (account, "weaponAmmo7", getPedTotalAmmo (source, 7)) 
      setAccountData (account, "weaponAmmo8", getPedTotalAmmo (source, 8))
      setAccountData (account, "weaponAmmo9", getPedTotalAmmo (source, 9)) 
      setAccountData (account, "weaponAmmo10", getPedTotalAmmo (source, 10)) 
      setAccountData (account, "weaponAmmo11", getPedTotalAmmo (source, 11)) 
      setAccountData (account, "weaponAmmo12", getPedTotalAmmo (source, 12)) 
  end
end 
end
addEventHandler ("onPlayerQuit", getRootElement(), onQuit) 

 

Edited by Shaman123
Link to post

Oh, sorry! I thought you had missing stuff.
About the saving here is how it will happen:

setAccountData (account, "weaponID7", getPedWeapon (source, 7))
if getPedWeapon(source, 7) == 37 then -- if the player has flamethrower
	setAccountData (account, "weaponAmmo7", getPedTotalAmmo (source, 7)/10)
else
	setAccountData (account, "weaponAmmo7", getPedTotalAmmo (source, 7))
end
--and the rest of the code...

You won't have to edit anything on the load part as the flame ammo is already divided and it should be all good.

Edited by SpecT
  • Like 1
Link to post

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