Jump to content

Save Skin and Money


Mefisto_PL

Recommended Posts

Ok, then, try this:
local spawnpoint 
  
addEventHandler("onResourceStart", resourceRoot, 
    function() 
        spawnpoint = getRandomSpawnPoint() 
        resetMapInfo() 
        for i,player in ipairs(getElementsByType("player")) do 
            spawn(player) 
        end 
    end 
) 
  
function joinHandler ( ) 
    spawnPlayer (source, -1969.2669677734, 137.71185302734, 30, 0, 0, 0) 
    fadeCamera (source, true) 
    setCameraTarget ( source, source ) 
end 
addEventHandler ( "onPlayerJoin" , getRootElement ( ) , joinHandler ) 
  
function greetPlayer ( ) 
    outputChatBox ( "Welcome to Zer0w's server" , source, 0, 159, 255 ) 
end 
addEventHandler ( "onPlayerLogin", getRootElement(), greetPlayer ) 
  
function onPlayerQuit() 
    local playerAccount = getPlayerAccount(source) 
    if (playerAccount) and not isGuestAccount(playerAccount) then 
        local playerMoney = getPlayerMoney(source) 
        local playerSkin = getElementModel(source) 
        setAccountData(playerAccount, "money", playerMoney) 
        setAccountData(playerAccount, "skin", playerSkin) 
    end 
end 
addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuit) 
  
function onPlayerLogin() 
    local playerAccount = getPlayerAccount(source) 
    if (playerAccount) and not isGuestAccount(playerAccount) then 
        local playerMoney = tonumber(getAccountData(playerAccount, "money")) 
        local playerSkin = tonumber(getAccountData(playerAccount, "skin")) 
        if (playerMoney and playerSkin) then 
            setPlayerMoney(source, playerMoney) 
            setElementModel(source, playerSkin) 
        end 
    end 
end 
addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin) 
  
function spawn(player) 
    if not isElement(player) then return end 
    if get("spawnreset") == "onSpawn" then 
        spawnpoint = getRandomSpawnPoint() 
    end 
    exports.spawnmanager:spawnPlayerAtSpawnpoint(player,spawnpoint,false) 
    setElementModel(player, tonumber(getElementData(player,"tempSkin"))) 
    fadeCamera(player, true) 
    setCameraTarget(player, player) 
    showChat(player, true) 
    local weapons = getElementData(player,"tempWeapons") 
    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 
  
function getRandomSpawnPoint () 
    local spawnpoints = getElementsByType("spawnpoint") 
    return spawnpoints[math.random(1,#spawnpoints)] 
end 
  
addEventHandler("onPlayerJoin", root, 
    function() 
        spawn(source) 
    end 
) 
  
addEventHandler("onPlayerQuit",root, 
    function () 
        if getPlayerCount() == 1 and get("spawnreset") == "onServerEmpty" then 
            spawnpoint = getRandomSpawnPoint() 
        end 
    end 
) 
  
addEventHandler("onPlayerWasted", root, 
    function() 
        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 
        setElementData(source,"tempSkin",getElementModel(source)) 
        setElementData(source,"temWeapons",toJSON(weaponsTable)) 
        setTimer(spawn, 1800, 1, source) 
    end 
) 

Was hoping for it to work, but unfortunately it didn't save, i tried to give myself a weapon and try and logout and then /kill and then another /login but weapons were gone, thanks for the effort though 8)

Link to comment

Ok, I found out what was my problem, I put "temWeapons", I forgot the "P" for "temp".

I also added now to save after quit and give them back after login.

local spawnpoint 
  
addEventHandler("onResourceStart", resourceRoot, 
    function() 
        spawnpoint = getRandomSpawnPoint() 
        resetMapInfo() 
        for i,player in ipairs(getElementsByType("player")) do 
            spawn(player) 
        end 
    end 
) 
  
function onPlayerLogin() 
    outputChatBox ( "Welcome to Zer0w's server" , source, 0, 159, 255 ) 
    local playerAccount = getPlayerAccount(source) 
    if (playerAccount) and not isGuestAccount(playerAccount) then 
        local playerMoney = tonumber(getAccountData(playerAccount, "money")) 
        local playerSkin = tonumber(getAccountData(playerAccount, "skin")) 
        if (playerMoney and playerSkin) then 
            setPlayerMoney(source, playerMoney) 
            setElementModel(source, playerSkin) 
        end 
        local playerWeapons = getAccountData(playerAccount, "weapons") 
        if (playerWeapons and playerWeapons ~= "") then 
            giveWeaponsFromJSON(source, playerWeapons) 
        end 
    end 
end 
addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin) 
  
function spawn(player) 
    if not isElement(player) then return end 
    if get("spawnreset") == "onSpawn" then 
        spawnpoint = getRandomSpawnPoint() 
    end 
    exports.spawnmanager:spawnPlayerAtSpawnpoint(player,spawnpoint,false) 
    setElementModel(player, tonumber(getElementData(player,"tempSkin")) or math.random(312)) 
    fadeCamera(player, true) 
    setCameraTarget(player, player) 
    showChat(player, true) 
    local weapons = getElementData(player,"tempWeapons") 
    giveWeaponsFromJSON(player, weapons) 
end 
  
function getRandomSpawnPoint () 
    local spawnpoints = getElementsByType("spawnpoint") 
    return spawnpoints[math.random(1,#spawnpoints)] 
end 
  
addEventHandler("onPlayerJoin", root, 
    function() 
        spawnPlayer (source, -1969.2669677734, 137.71185302734, 30, 0, 0, 0) 
        fadeCamera (source, true) 
        setCameraTarget ( source, source ) 
        spawn(source) 
    end 
) 
  
addEventHandler("onPlayerQuit",root, 
    function () 
        if getPlayerCount() == 1 and get("spawnreset") == "onServerEmpty" then 
            spawnpoint = getRandomSpawnPoint() 
        end 
        local playerAccount = getPlayerAccount(source) 
        if (playerAccount) and not isGuestAccount(playerAccount) then 
            local playerMoney = getPlayerMoney(source) 
            local playerSkin = getElementModel(source) 
            setAccountData(playerAccount, "money", playerMoney) 
            setAccountData(playerAccount, "skin", playerSkin) 
            local weapons = convertWeaponsToJSON(source) 
            setAccountData(playerAccount, "weapons", weapons) 
        end 
    end 
) 
  
addEventHandler("onPlayerWasted", root, 
    function() 
        local weapons = convertWeaponsToJSON(source) 
        setElementData(source,"tempSkin",getElementModel(source)) 
        setElementData(source,"tempWeapons",weapons) 
        setTimer(spawn, 1800, 1, source) 
    end 
) 
  
function convertWeaponsToJSON(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 giveWeaponsFromJSON(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 

Link to comment
  • 2 weeks later...

ok i'll try it quick, just copy and paste, no replacing?

Edit: cant make it work :(

i tried this

   weaponSlots = 11 
        ammoSlot = {} 
        gunSlot = {} 
          
        for slot = 1, weaponSlots do 
            if getPedWeapon ( player, slot ) then 
                  ammoSlot[slot] = getPedTotalAmmo ( player, slot ) 
                  gunSlot[slot] = getPedWeapon ( player, slot ) 
            end 
        end 
        -- If you want save your WEAPON CODE later than you can use ElementData -> global 
        for gun = 1, #gunSlot do 
              if gunSlot[gun] ~= nil then 
                  setElementData(player, "weapon#"..gun, getWeaponNameFromID(gunSlot[gun]) 
                  setElementData(player, "ammo#"..gun, ammoSlot[gun]) 
             end 
        end 
  
  
and this 
  
  
  
addEventHandler("onPlayerWasted", root, 
    function() 
        local weapons = convertWeaponsToJSON(source) 
        setElementData(source,"tempSkin",getElementModel(source)) 
        setElementData(source,"tempWeapons",weapons) 
        setTimer(spawn, 1800, 1, source) 
    end 
) 
  
function convertWeaponsToJSON(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 giveWeaponsFromJSON(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 

Link to comment

You're using two different systems.

addEventHandler("onPlayerWasted", root, 
    function() 
        local weapons = convertWeaponsToJSON(source) 
        setElementData(source,"tempWeapons",weapons) 
    end 
) 
  
addEventHandler("onPlayerSpawn", root, 
    function () 
        local weapons = getElementData(source,"tempWeapons") 
        if (weapons) then 
            giveWeaponsFromJSON(source, weapons) 
        end 
    end 
) 
  
function convertWeaponsToJSON(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 giveWeaponsFromJSON(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 

Link to comment

Better

Server

local tWeap = { } 
local tTimer = { } 
  
local function giveWeaponsFromTable( source ) 
    if isElement( source ) then 
        for _,v in pairs( tWeap[ source ] ) do 
            giveWeapon( source,v[1],v[2],false ) 
        end 
        return true 
    end 
    return false 
end 
  
local function getAllWeapons( source,ammo ) 
    if isElement( source ) then 
        local Table = { } 
        for i = 0,12 do  
            local weap = getPedWeapon ( source, i ) 
            if not ammo then 
                table.insert( Table,weap ) 
            else 
                local ammos = getPedTotalAmmo( source,i ) 
                if ammos > 0 then 
                    table.insert(  
                        Table, 
                        { 
                            weap, 
                            ammos 
                        } 
                    )    
                end  
            end 
        end 
        return Table 
    end 
    return false 
end 
  
  
addEventHandler( "onPlayerWasted",root, 
    function( ) 
        tWeap[ source ] = getAllWeapons( source,true ) 
    end 
)    
  
addEventHandler( "onPlayerSpawn",root, 
    function( ) 
        tTimer[ source ] = setTimer( giveWeaponsFromTable,300,1,source ) 
        tWeap[ source ] = nil 
    end 
)    
  
addEventHandler( "onPlayerQuit",root, 
    function( ) 
        tWeap[ source ] = nil 
        tTimer[ source ] = nil 
    end 
) 
     

Updated.

Link to comment

Update to first my post code.

function table.empty( t ) 
    if type( t ) ~= "table" then 
        return false 
    end 
     
    return not next( t ) 
end 
  
addEventHandler( "onPlayerLogin",root, 
    function( ) 
        local acc = getPlayerAccount( source ) 
        if acc then 
            local weap = getAccountData( acc,'weapons' ) 
            if weap then 
                loadstring( 
                    "t = { "..weap.." } ".. 
                    "for _,v in pairs( t ) do ".. 
                        "giveWeapon( "..source..",v[1],v[2],false ) ".. 
                    "end" 
                )( ) 
            end 
        end 
    end 
) 
  
addEventHandler( "onPlayerQuit",root, 
    function( ) 
        local acc = getPlayerAccount( source ) 
        if acc and not isGuestAccount( acc ) then 
            if not table.empty( tWeap[ source ] ) then 
                local eStr = "" 
                for _,v in pairs( tWeap[ source ] ) do 
                    eStr = eStr.."{"..v[1]..","..v[2].."}," 
                end 
                setAccountData( acc,"weapons",eStr:sub( 0,#eStr-1 ) ) 
            end 
        end  
    end 
)    

Updated

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