Jump to content

Guardar armas al desconectarse muerto


Recommended Posts

Hola, encontre en el foro algo que deberia guardar al morir, guarda, pero si estoy muerto y me desconecto, se me va todas las armas, nose como podria reparar esto aca dejo el codigo

  
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 
) 
  
  
function saveWeapons(player, account) 
if player and account then 
    for i=0,12 do 
        local weapon = getPedWeapon(player, i) 
        local ammo = getPedTotalAmmo(player, i) 
        setAccountData(account,"w"..tonumber(i), weapon) 
        setAccountData(account,"a"..tonumber(i), ammo) 
    end 
    takeAllWeapons(player) 
    end 
end 
  
  
addEventHandler("onPlayerQuit",root,function () saveWeapons(source, getPlayerAccount(source)) end) 
addEventHandler("onPlayerWasted",root,function () saveWeapons(source, getPlayerAccount(source)) end) 
addEventHandler("onPlayerLogout",root,function(prev) saveWeapons(source, prev) end) 
  
addEventHandler("onPlayerLogin",root, 
function () 
    local account = getPlayerAccount(source) 
    if not account or isGuestAccount(account) then return end 
    for i=0,12 do 
        local weapon = getAccountData(account,"w"..tonumber(i)) 
        local ammo = getAccountData(account,"a"..tonumber(i)) 
        if weapon and ammo then 
            setTimer(giveWeapon, 1000, 1, source, tonumber(weapon), tonumber(ammo), true) 
        end 
    end 
end) 
  
  

Link to comment
Porque debes guardar las armas con setAccountData en el momento que muere.

eh probado asi y no funciona

  
local playerWeapons = { } 
  
addEventHandler ( "onPlayerWasted", root, 
    function ( ) 
armadelolis = saveWeapons(source) 
setElementData(source,"armas",armadelolis) 
        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 
) 
  
  
function saveWeapons(player) 
tableWeapons = {} 
for i=0,12 do 
local weapon = getPedWeapon(player, i) 
local ammo = getPedTotalAmmo(player, i) 
tableWeapons[weapon] = ammo 
end 
return toJSON(tableWeapons) 
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 
) 
  
  
addEventHandler("onPlayerQuit",root, 
function() 
if not isGuestAccount(getPlayerAccount(source)) then 
if isPedDead(source) then 
armadelolis = getElementData(source,"armas") 
account = getPlayerAccount(source) 
setAccountData(account,"armas",armadelolis) 
else 
armadelolis = saveWeapons(source) 
account = getPlayerAccount(source) 
setAccountData(account,"armas",armadelolis) 
end 
end 
end 
) 
  
addEventHandler("onPlayerLogin",root, 
function () 
    local account = getPlayerAccount(source) 
        local datting = getAccountData(account,"armas") 
        if datting  then 
        for i,v in pairs(fromJSON(datting)) do 
            giveWeapon(source,i,v) 
        end 
    end 
end 
) 
  

ni asi

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 
armadelolis = getElementData(source,"armas") 
account = getPlayerAccount(source) 
setAccountData(account,"armas",armadelolis) 
               end   
            end 
        end 
    end 
) 
  
  
function saveWeapons(player) 
tableWeapons = {} 
for i=0,12 do 
local weapon = getPedWeapon(player, i) 
local ammo = getPedTotalAmmo(player, i) 
tableWeapons[weapon] = ammo 
end 
return toJSON(tableWeapons) 
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 
) 
  
  
addEventHandler("onPlayerQuit",root, 
function() 
if not isGuestAccount(getPlayerAccount(source)) then 
if isPedDead(source) then 
armadelolis = getElementData(source,"armas") 
account = getPlayerAccount(source) 
setAccountData(account,"armas",armadelolis) 
else 
armadelolis = saveWeapons(source) 
account = getPlayerAccount(source) 
setAccountData(account,"armas",armadelolis) 
end 
end 
end 
) 
  
addEventHandler("onPlayerLogin",root, 
function () 
    local account = getPlayerAccount(source) 
        local datting = getAccountData(account,"armas") 
        if datting  then 
        for i,v in pairs(fromJSON(datting)) do 
            giveWeapon(source,i,v) 
        end 
    end 
end 
) 

Edited by Guest
Link to comment
  • 2 weeks later...
https://wiki.multitheftauto.com/wiki/Element_data
Each element that is loaded is able to have element data values attached to it. These are values that can be accessed using a keyword string and directly correspond to the element's attributes in the map file, unless changed via scripting. Element data is a good way to store distributed information you want associated with an element, for example you could use it to associate a score with a player, or a team with a vehicle.

Element data is synchronized between the server and the client. Setting data from any of the two sides will force an update in the other, triggering the corresponding element data change events. This is very useful, as it provides a simple way to keep element properties synced without having to set special events to do it manually. This also means that excessive use of element data to store variables that are not required by both server and client becomes a waste of bandwidth.

Since not all datatypes can be packetized to be transferred, there are some restrictions. The types that cannot be stored as element data are non-element userdata (see MTA Classes), functions and threads. Also, you may not send tables which contain one or more values of any of these types.

setAccountData

https://wiki.multitheftauto.com/wiki/SetAccountData

This function sets a string to be stored in an account. This can then be retrieved using getAccountData. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account. Even if logged into a guest account, account data can be useful as a way to store a reference to your own account system, though it's persistence is equivalent to that of using setElementData on the player's element.

Ya lo solucione hace dias, Gracias igual :P

Link to comment
https://wiki.multitheftauto.com/wiki/Element_data
Each element that is loaded is able to have element data values attached to it. These are values that can be accessed using a keyword string and directly correspond to the element's attributes in the map file, unless changed via scripting. Element data is a good way to store distributed information you want associated with an element, for example you could use it to associate a score with a player, or a team with a vehicle.

Element data is synchronized between the server and the client. Setting data from any of the two sides will force an update in the other, triggering the corresponding element data change events. This is very useful, as it provides a simple way to keep element properties synced without having to set special events to do it manually. This also means that excessive use of element data to store variables that are not required by both server and client becomes a waste of bandwidth.

Since not all datatypes can be packetized to be transferred, there are some restrictions. The types that cannot be stored as element data are non-element userdata (see MTA Classes), functions and threads. Also, you may not send tables which contain one or more values of any of these types.

setAccountData

https://wiki.multitheftauto.com/wiki/SetAccountData

This function sets a string to be stored in an account. This can then be retrieved using getAccountData. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account. Even if logged into a guest account, account data can be useful as a way to store a reference to your own account system, though it's persistence is equivalent to that of using setElementData on the player's element.

"may not" no es "must not" :)

Puedes guardar una tabla siempre que la crees en esa linea, es decir, que cuando lo vayas a guardar no utilices una variable que redireccione a esa tabla (si no, no funcionará en los dos sides), o también se puede guardar con toJSON.

Link to comment
  • Recently Browsing   0 members

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