Jump to content

Losing weapons on second death HELP!!! plz :D maybe???


kewizzle

Recommended Posts

sorry i keep posting here im not a noob to scripting mta resources but im also not a god.. with that said i got an error on the second death and it said 

[18:10:05] ERROR: zombies\save\saver.lua:17: bad argument #1 to 'pairs' (table e
xpected, got nil)

and i lost all my weapons. 

local tempData = { } 
      
    addEventHandler ( "onPlayerWasted", getRootElement(), 
        function ( ) 
            tempData [ source ] = 
                { 
                    weapons = getWeaponsTable ( source ), 
                    skin = getElementModel ( source ) 
                } 
        end 
    ) 
      
    addEventHandler ( "onPlayerSpawn", getRootElement(), 
        function ( ) 
            if ( tempData [ source ] ) then 
                setElementModel ( source, tempData [ source ].skin ) 
                for weapon, ammo in pairs ( tempData [ source ].weapons ) do 
                    giveWeapon ( source, weapon, ammo, true ) 
                end 
            end 
        end 
    ) 
      
    function getWeaponsTable ( thePlayer ) 
        local weapons = { } 
        local hasAnyWeapon = false 
        for slot = 0, 12 do 
            local weapon = getPedWeapon ( thePlayer, slot ) 
            if ( weapon > 0 ) then 
                local ammo = getPedTotalAmmo ( thePlayer, slot ) 
                if ( ammo > 0 ) then 
                    weapons [ weapon ] = ammo 
                    hasAnyWeapon = true 
                end 
            end 
        end 
        if ( hasAnyWeapon ) then 
            return weapons 
        end 
    end 

(yes i got this script from someone else.)

Link to comment
addEventHandler("onPlayerSpawn",root,function ()
    local account = getPlayerAccount(source)
    if (account) and not (isGuestAccount(account)) then
        local weapons = getAccountData(account, "weapns")
        if (weapons) and (fromJSON(weapons)) then
            takeAllWeapons(source)
            for weapon, ammo in pairs(fromJSON(weapons)) do
                giveWeapon(source, weapon, ammo, true)
            end
        end
    end 
end)

addEventHandler("onPlayerWasted",root,function ()
    local account = getPlayerAccount(source)
    if (account) and not (isGuestAccount(account)) then
        local weapons = getAllPedWeapon(source)
        setAccountData(account, "weapns", toJSON(weapons))
    end
end)


function getAllPedWeapon(thePed)
    local weapons = { }
    for slot=1, 12 do
        local weapon = getPedWeapon(thePed, slot)
        local ammo = getPedTotalAmmo(thePed, slot)
        if (weapon > 0) and (ammo > 0) then
            weapons[weapon] = ammo
        end
    end
    return weapons
end

 

Link to comment

	local tempData = { } 
      
    addEventHandler ( "onPlayerWasted", getRootElement(), 
        function ( ) 
            tempData [ source ] = 
                { 
                    weapons = getWeaponsTable ( source ), 
                    skin = getElementModel ( source ) 
                } 
        end 
    ) 
      
    addEventHandler ( "onPlayerSpawn", getRootElement(), 
        function ( ) 
            if ( tempData [ source ] ) then 
                setElementModel ( source, tempData [ source ].skin ) 
                for weapon, ammo in pairs ( tempData [ source ].weapons ) do 
                    giveWeapon ( source, weapon, ammo, true ) 
                end
                tempData[ source ] = nil
            end 
        end 
    ) 
      
    function getWeaponsTable ( thePlayer ) 
        local weapons = { } 
        local hasAnyWeapon = false 
        for slot = 0, 12 do 
            local weapon = getPedWeapon ( thePlayer, slot ) 
            if ( weapon > 0 ) then 
                local ammo = getPedTotalAmmo ( thePlayer, slot ) 
                if ( ammo > 0 ) then 
                    weapons [ weapon ] = ammo 
                    hasAnyWeapon = true 
                end 
            end 
        end 
        if ( hasAnyWeapon ) then 
            return weapons 
        end 
    end 
	

Edited by Anubhav
Link to comment

neither script works, if youd like join my server and test for yourself. It's in my sig.

this is my spawn script aswell.

outputServerLog ( "***Spawn Script Loaded  ***" )
local spawnX, spawnY, spawnZ = math.random(-694,-679), math.random(951,963), 17
function joinHandler()
             spawnPlayer(source, spawnX, spawnY, spawnZ)	
             fadeCamera(source, true)
	setCameraTarget(source, source)
             outputChatBox("#FF00DC"..getPlayerName(source).." #ff0000Welcome To K#ff4000e#ff7f00w#ffbf00i#ffff00z#00ff00z#00ff80l#00ffffe#0080ff'#0000ffs Zombie Server, :O :O. Press \"F9\" For HELP", source, r, g, b, true)
end
function deathHandler()
             spawnPlayer(source, spawnX, spawnY, spawnZ)	
             fadeCamera(source, true)
	setCameraTarget(source, source)
end
addEventHandler("onPlayerJoin", getRootElement(), joinHandler)
addEventHandler("onPlayerWasted", getRootElement(), deathHandler)

function giveWeaponsOnSpawn ( theSpawnpont, theTeam )
getammo = getPedTotalAmmo ( source, 2 )
if (getammo <= 14) then
    giveWeapon ( source, 22, 15 ) 
	end
end
addEventHandler ( "onPlayerSpawn", getRootElement(), giveWeaponsOnSpawn )

I dont think it would bother it because even if my ammo is >= 15 it still doesnt work lol.

Link to comment

Tried scripting this but does not work.

addEventHandler ( "onPlayerWasted", getRootElement(), function()
		gun0 = getPedWeapon ( source, 0 )
		gun1 = getPedWeapon ( source, 1 )
		gun2 = getPedWeapon ( source, 2 )
		gun2a = getPedTotalAmmo ( source, 2 )
		gun3 = getPedWeapon ( source, 3 )
		gun3a = getPedTotalAmmo ( source, 3 )
		gun4 = getPedWeapon ( source, 4 )
		gun4a = getPedTotalAmmo ( source, 4 )
		gun5 = getPedWeapon ( source, 5 )
		gun5a = getPedTotalAmmo ( source, 5 )
		gun6 = getPedWeapon ( source, 6 )
		gun6a = getPedTotalAmmo ( source, 6 )
		gun7 = getPedWeapon ( source, 7 )
		gun7a = getPedTotalAmmo ( source, 7 )
		gun8 = getPedWeapon ( source, 8 )
		gun8a = getPedTotalAmmo ( source, 8 )
		gun9 = getPedWeapon ( source, 9 )
		gun9a = getPedTotalAmmo ( source, 9 )
		gun10 = getPedWeapon ( source, 10 )
		gun11 = getPedWeapon ( source, 11 )
		gun12 = getPedWeapon ( source, 12 )

addEventHandler ( "onPlayerSpawn", getRootElement(), function()
		giveWeapon( source, gun0, 1)
		giveWeapon( source, gun1, 1)
		giveWeapon( source, gun2, gun2a)
		giveWeapon( source, gun3, gun3a)
		giveWeapon( source, gun4, gun4a)
		giveWeapon( source, gun5, gun5a)
		giveWeapon( source, gun6, gun6a)
		giveWeapon( source, gun7, gun7a)
		giveWeapon( source, gun8, gun8a)
		giveWeapon( source, gun9, gun9a)
		giveWeapon( source, gun10, 1)
		giveWeapon( source, gun11, 1)
		giveWeapon( source, gun12, 1)
	end)
end)

 

Link to comment
1 hour ago, NeXuS™ said:

Added to TODO list, going to do it today for ya.

thank you so much once this issue is done then i should be good. I have literally tried coding it and tried using other resources and peoples code and nothing worked at all. I literally tried everyones code and resource and coded it in a few different ways and nothing. LOL all it did is add more ammo to my pistol on spawn. If you get this to work i would be happy lol.

Link to comment
1 hour ago, NeXuS™ said:

Meanwhile I'm gone, could you express what should the script do at all?

I have a zombie server, in this server you can collect weapons when zombies have died, you spawn with 15 colt 45 ammo if your ammo is <= 14. When you spawn you're supposed to still have all the weapons and ammo you picked up from dead zombies so you can sell it all in the shop for $1 per bullet. Players are supposed to save up their ammo to sell and make cash. I got all this coded however, in my server weapons we're saving when onPlayerWasted when they died, but if the player dies a second time without collecting any more ammo they will lose all the guns and that isnt supposed to happen. I wan't the script to save all the players weapons on death everytime and respawn with the ammo and guns they picked up while killing zombies. Basically onPlayerWasted save the weapons in each slot and when onPlayerSpawn returns the weapons the player had when he died with the correct ammo. 

 

Now i dont know if any of my scripts will effect anything but ill send you my onPlayerSpawn script i currently have for the giveWeapon when they have <= 14 colt 45 ammo

outputServerLog ( "***Spawn Script Loaded  ***" )
local PlayersTeam = createTeam ( "Players" )
setTeamFriendlyFire ( PlayersTeam, false ) 
local spawnX, spawnY, spawnZ = math.random(-694,-679), math.random(951,963), 17
function joinHandler()
             spawnPlayer(source, spawnX, spawnY, spawnZ)	
             fadeCamera(source, true)
	setCameraTarget(source, source)
	setPlayerTeam(source, PlayersTeam )
             outputChatBox("#FF00DC"..getPlayerName(source).." #ff0000Welcome To K#ff4000e#ff7f00w#ffbf00i#ffff00z#00ff00z#00ff80l#00ffffe#0080ff'#0000ffs Zombie Server, :O :O. Press \"F9\" For HELP", source, r, g, b, true)
end
function deathHandler()
             spawnPlayer(source, spawnX, spawnY, spawnZ)	
             fadeCamera(source, true)
	setCameraTarget(source, source)
end
addEventHandler("onPlayerWasted", getRootElement(), deathHandler)

function giveWeaponsOnSpawn ( theSpawnpont, theTeam )
getammo = getPedTotalAmmo ( source, 2 )
if (getammo <= 14) then
    giveWeapon ( source, 22, 15 ) 
	end
end
addEventHandler ( "onPlayerSpawn", getRootElement(), giveWeaponsOnSpawn )


addEventHandler ( "onPlayerLogin", getRootElement(), joinHandler )

the file for this is 

<script src="AZZ/spawns.lua" />

 

Link to comment
3 hours ago, kewizzle said:

I have a zombie server, in this server you can collect weapons when zombies have died, you spawn with 15 colt 45 ammo if your ammo is <= 14. When you spawn you're supposed to still have all the weapons and ammo you picked up from dead zombies so you can sell it all in the shop for $1 per bullet. Players are supposed to save up their ammo to sell and make cash. I got all this coded however, in my server weapons we're saving when onPlayerWasted when they died, but if the player dies a second time without collecting any more ammo they will lose all the guns and that isnt supposed to happen. I wan't the script to save all the players weapons on death everytime and respawn with the ammo and guns they picked up while killing zombies. Basically onPlayerWasted save the weapons in each slot and when onPlayerSpawn returns the weapons the player had when he died with the correct ammo. 

 

Now i dont know if any of my scripts will effect anything but ill send you my onPlayerSpawn script i currently have for the giveWeapon when they have <= 14 colt 45 ammo


outputServerLog ( "***Spawn Script Loaded  ***" )
local PlayersTeam = createTeam ( "Players" )
setTeamFriendlyFire ( PlayersTeam, false ) 
local spawnX, spawnY, spawnZ = math.random(-694,-679), math.random(951,963), 17
function joinHandler()
             spawnPlayer(source, spawnX, spawnY, spawnZ)	
             fadeCamera(source, true)
	setCameraTarget(source, source)
	setPlayerTeam(source, PlayersTeam )
             outputChatBox("#FF00DC"..getPlayerName(source).." #ff0000Welcome To K#ff4000e#ff7f00w#ffbf00i#ffff00z#00ff00z#00ff80l#00ffffe#0080ff'#0000ffs Zombie Server, :O :O. Press \"F9\" For HELP", source, r, g, b, true)
end
function deathHandler()
             spawnPlayer(source, spawnX, spawnY, spawnZ)	
             fadeCamera(source, true)
	setCameraTarget(source, source)
end
addEventHandler("onPlayerWasted", getRootElement(), deathHandler)

function giveWeaponsOnSpawn ( theSpawnpont, theTeam )
getammo = getPedTotalAmmo ( source, 2 )
if (getammo <= 14) then
    giveWeapon ( source, 22, 15 ) 
	end
end
addEventHandler ( "onPlayerSpawn", getRootElement(), giveWeaponsOnSpawn )


addEventHandler ( "onPlayerLogin", getRootElement(), joinHandler )

the file for this is 


<script src="AZZ/spawns.lua" />

 

MY : WeaponSaver.lua

addEventHandler("onPlayerWasted", root, 
    function() 
        local weapons = getWeaponsOfPlayers(source) 
        setElementData(source,"isDead",true) 
        setElementData(source,"tWeapons",weapons) 
    end 
) 
  
addEventHandler("onPlayerSpawn", root, 
    function () 
        local weapons = getElementData(source,"tWeapons") 
        setElementData(source,"isDead",false) 
        if (weapons) then 
            giveWeaponsOfPlayer(source, weapons) 
        end 
    end 
) 
  
function getWeaponsOfPlayer(player) 
    local weaponSlots = 12 
    local weaponsTable = {} 
    for slot=1, weaponSlots do 
        local weapon = getPedWeapon( source, slot ) 
        local ammo = getPedTotalAmmo( source, slot ) 
        if (weapon > 0 and ammo > 0) then 
            weaponsTable[weapon] = ammo 
        end 
    end 
    return toJSON(weaponsTable) 
end 
  
function giveWeaponsOfPlayer(player, weapons) 
    if (weapons and weapons ~= "") then 
        for weapon, ammo in pairs(fromJSON(weapons)) do 
            if (weapon and ammo) then 
                giveWeapon(player, tonumber(weapon), tonumber(ammo)) 
            end 
        end 
    end 
end 

addEventHandler( "onPlayerLogin",root, 
    function( ) 
        local acc = getPlayerAccount( source ) 
        if acc then 
            local weap = getAccountData( acc,'Player:weapons' ) 
            if weap then 
               giveWeaponsOfPlayer(source, weap) 
            end 
        end 
    end 
) 
  
addEventHandler( "onPlayerQuit",root, 
    function( ) 
        local acc = getPlayerAccount( source ) 
        if acc and not isGuestAccount( acc ) then 
            if  getElementData(source,"isDead") == true then
            local weap = getElementData(source,"tWeapons")
            setAccountData( acc,"Player:weapons",weap ) 
            else
            local weap = getWeaponsOfPlayer(source)
            setAccountData( acc,"Player:weapons",weap )
            end
        end  
    end 
)    

P.S : Your spawn.lua

outputServerLog ( "***Spawn Script Loaded  ***" )

local PlayersTeam = createTeam ( "Players" )
setTeamFriendlyFire ( PlayersTeam, false ) 

local spawnX, spawnY, spawnZ = math.random(-694,-679), math.random(951,963), 17

function joinHandler()
    spawnPlayer(source, spawnX, spawnY, spawnZ)	
    fadeCamera(source, true)
	setCameraTarget(source, source)
	setPlayerTeam(source, PlayersTeam )
    outputChatBox("#FF00DC"..getPlayerName(source).." #ff0000Welcome To K#ff4000e#ff7f00w#ffbf00i#ffff00z#00ff00z#00ff80l#00ffffe#0080ff'#0000ffs Zombie Server, :O :O. Press \"F9\" For HELP", source, r, g, b, true)
end
addEventHandler ( "onPlayerLogin", getRootElement(), joinHandler )

function deathHandler()
   setTimer( function(source) 
    spawnPlayer(source, spawnX, spawnY, spawnZ)	
    fadeCamera(source, true)
   	setCameraTarget(source, source)
    end,2000,1,source) 
end
addEventHandler("onPlayerWasted", getRootElement(), deathHandler)

:) enjoy and please like if it fits your need:)

 

  • Like 1
Link to comment
6 hours ago, Ayush Rathore said:

MY : WeaponSaver.lua


addEventHandler("onPlayerWasted", root, 
    function() 
        local weapons = getWeaponsOfPlayers(source) 
        setElementData(source,"isDead",true) 
        setElementData(source,"tWeapons",weapons) 
    end 
) 
  
addEventHandler("onPlayerSpawn", root, 
    function () 
        local weapons = getElementData(source,"tWeapons") 
        setElementData(source,"isDead",false) 
        if (weapons) then 
            giveWeaponsOfPlayer(source, weapons) 
        end 
    end 
) 
  
function getWeaponsOfPlayer(player) 
    local weaponSlots = 12 
    local weaponsTable = {} 
    for slot=1, weaponSlots do 
        local weapon = getPedWeapon( source, slot ) 
        local ammo = getPedTotalAmmo( source, slot ) 
        if (weapon > 0 and ammo > 0) then 
            weaponsTable[weapon] = ammo 
        end 
    end 
    return toJSON(weaponsTable) 
end 
  
function giveWeaponsOfPlayer(player, weapons) 
    if (weapons and weapons ~= "") then 
        for weapon, ammo in pairs(fromJSON(weapons)) do 
            if (weapon and ammo) then 
                giveWeapon(player, tonumber(weapon), tonumber(ammo)) 
            end 
        end 
    end 
end 

addEventHandler( "onPlayerLogin",root, 
    function( ) 
        local acc = getPlayerAccount( source ) 
        if acc then 
            local weap = getAccountData( acc,'Player:weapons' ) 
            if weap then 
               giveWeaponsOfPlayer(source, weap) 
            end 
        end 
    end 
) 
  
addEventHandler( "onPlayerQuit",root, 
    function( ) 
        local acc = getPlayerAccount( source ) 
        if acc and not isGuestAccount( acc ) then 
            if  getElementData(source,"isDead") == true then
            local weap = getElementData(source,"tWeapons")
            setAccountData( acc,"Player:weapons",weap ) 
            else
            local weap = getWeaponsOfPlayer(source)
            setAccountData( acc,"Player:weapons",weap )
            end
        end  
    end 
)    

P.S : Your spawn.lua


outputServerLog ( "***Spawn Script Loaded  ***" )

local PlayersTeam = createTeam ( "Players" )
setTeamFriendlyFire ( PlayersTeam, false ) 

local spawnX, spawnY, spawnZ = math.random(-694,-679), math.random(951,963), 17

function joinHandler()
    spawnPlayer(source, spawnX, spawnY, spawnZ)	
    fadeCamera(source, true)
	setCameraTarget(source, source)
	setPlayerTeam(source, PlayersTeam )
    outputChatBox("#FF00DC"..getPlayerName(source).." #ff0000Welcome To K#ff4000e#ff7f00w#ffbf00i#ffff00z#00ff00z#00ff80l#00ffffe#0080ff'#0000ffs Zombie Server, :O :O. Press \"F9\" For HELP", source, r, g, b, true)
end
addEventHandler ( "onPlayerLogin", getRootElement(), joinHandler )

function deathHandler()
   setTimer( function(source) 
    spawnPlayer(source, spawnX, spawnY, spawnZ)	
    fadeCamera(source, true)
   	setCameraTarget(source, source)
    end,2000,1,source) 
end
addEventHandler("onPlayerWasted", getRootElement(), deathHandler)

:) enjoy and please like if it fits your need:)

 

it works but my player doesnt spawn with a colt 45 when ammo is below 15.

ill add this to on player spawn and let you know if it works

EDIT: it did not.

getammo = getPedTotalAmmo ( source, 2 )
if (getammo <= 14) then
    giveWeapon ( source, 22, 15 ) 
	end
end

 

Edited by kewizzle
Link to comment

i got it working!

outputServerLog ( "***Spawn Script Loaded  ***" )

local PlayersTeam = createTeam ( "Players" )
setTeamFriendlyFire ( PlayersTeam, false ) 

local spawnX, spawnY, spawnZ = math.random(-694,-679), math.random(951,963), 15

function joinHandler()
    spawnPlayer(source, spawnX, spawnY, spawnZ)	
    fadeCamera(source, true)
	setCameraTarget(source, source)
	setPlayerTeam(source, PlayersTeam )
    outputChatBox("#FF00DC"..getPlayerName(source).." #ff0000Welcome To K#ff4000e#ff7f00w#ffbf00i#ffff00z#00ff00z#00ff80l#00ffffe#0080ff'#0000ffs Zombie Server, :O :O. Press \"F9\" For HELP", source, r, g, b, true)
end
addEventHandler ( "onPlayerLogin", getRootElement(), joinHandler )

function deathHandler()
   setTimer( function(source) 
    spawnPlayer(source, spawnX, spawnY, spawnZ)	
    fadeCamera(source, true)
   	setCameraTarget(source, source)
	getammo = getPedTotalAmmo ( source, 2 )
	if (getammo <= 14) then
    giveWeapon ( source, 22, 15 ) 
	end

    end,1000,1,source) 
end
addEventHandler("onPlayerWasted", getRootElement(), deathHandler)

thanks all!

Link to comment
16 hours ago, kewizzle said:

i got it working!


outputServerLog ( "***Spawn Script Loaded  ***" )

local PlayersTeam = createTeam ( "Players" )
setTeamFriendlyFire ( PlayersTeam, false ) 

local spawnX, spawnY, spawnZ = math.random(-694,-679), math.random(951,963), 15

function joinHandler()
    spawnPlayer(source, spawnX, spawnY, spawnZ)	
    fadeCamera(source, true)
	setCameraTarget(source, source)
	setPlayerTeam(source, PlayersTeam )
    outputChatBox("#FF00DC"..getPlayerName(source).." #ff0000Welcome To K#ff4000e#ff7f00w#ffbf00i#ffff00z#00ff00z#00ff80l#00ffffe#0080ff'#0000ffs Zombie Server, :O :O. Press \"F9\" For HELP", source, r, g, b, true)
end
addEventHandler ( "onPlayerLogin", getRootElement(), joinHandler )

function deathHandler()
   setTimer( function(source) 
    spawnPlayer(source, spawnX, spawnY, spawnZ)	
    fadeCamera(source, true)
   	setCameraTarget(source, source)
	getammo = getPedTotalAmmo ( source, 2 )
	if (getammo <= 14) then
    giveWeapon ( source, 22, 15 ) 
	end

    end,1000,1,source) 
end
addEventHandler("onPlayerWasted", getRootElement(), deathHandler)

thanks all!

Your welcome Happy Scripting :)

 

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