main Posted November 17, 2019 Share Posted November 17, 2019 (edited) Olá pessoal, estou com uma dúvida de como posso fazer para inserir e retirar mais de 1 no painel de inventário da casa, pois só estou conseguindo fazer pra setar 1 arma. OBS: Estou setando na database em toJSON Imagem do painel para melhor entendimento do funcionamento: https://imgur.com/a/DNtKdpv addEvent ( "housing:addWeaponHouse", true ) addEventHandler ( "housing:addWeaponHouse", resourceRoot, function ( weaponID, ammo ) local houseName = playersInInterior [ client ] if houseName then local table = fromJSON( getHouseData( houseName, "houseWeapons" ) ) if table [ tostring( weaponID ) ] then return outputChatBox ( "Housing manager: Você já tem essa arma armazenada!", client, 255, 0, 0 ) end takeWeapon( client, weaponID ) setHouseData ( houseName, "houseWeapons", toJSON( setPedWeaponHouse( client, weaponID, ammo ) ) ) triggerClientEvent( "housing:updateWeaponsManager", resourceRoot, getPedWeapons( client ), getHouseData( houseName, "houseWeapons" ) ) end end ) addEvent ( "housing:removeWeaponHouse", true ) addEventHandler ( "housing:removeWeaponHouse", resourceRoot, function ( weaponID, ammo ) local houseName = playersInInterior [ client ] if houseName then giveWeapon( client, weaponID, ammo, true ) triggerClientEvent( "housing:updateWeaponsManager", resourceRoot, getPedWeapons( client ), getHouseData( houseName, "houseWeapons" ) ) end end ) function setPedWeaponHouse(ped, wep, ammo) local playerWeapons = {} if isElement(ped) and getElementType(ped) == "player" then playerWeapons[wep] = ammo else return false end return playerWeapons end function getPedWeapons(ped) local playerWeapons = {} if isElement(ped) and getElementType(ped) == "player" then for i=1,9 do local wep = getPedWeapon(ped,i) local ammo = getPedTotalAmmo(ped,i) if wep ~= 0 then playerWeapons[wep] = ammo end end else return false end return playerWeapons end Edited November 17, 2019 by MainSCR Link to comment
[M]ister Posted November 17, 2019 Share Posted November 17, 2019 A ideia é sempre obter o que já está salvo, efetuar as operações em cima do que obteve e então salvar todos novamente. -- Exemplo addCommandHandler("inventario", function(thePlayer, _, action, WeaponID, ammo) local houseName = playersInInterior[thePlayer] local itens = fromJSON(getHouseData(houseName, "houseWeapons")) if (action == "incluir") itens[WeaponID] = ammo else if (action == "remover") then itens[WeaponID] = nil end setHouseData(houseName, "houseWeapons", toJSON(itens)) end ) 1 Link to comment
main Posted November 17, 2019 Author Share Posted November 17, 2019 @MaligNos Isso que estava precisando e não sabia como fazer! Na parte de incluir deu certo, porém na parte de remover não obtive sucesso O que tentei abaixo: addEvent ( "housing:removeWeaponHouse", true ) addEventHandler ( "housing:removeWeaponHouse", resourceRoot, function ( weaponID, ammo ) local houseName = playersInInterior [ client ] if houseName then local houseData = getHouseData( houseName, "houseWeapons" ) if houseData then local itens = fromJSON( houseData ) giveWeapon( client, weaponID, ammo, true ) itens [ weaponID ] = nil setHouseData ( houseName, "houseWeapons", toJSON ( itens ) ) triggerClientEvent( "housing:updateWeaponsManager", resourceRoot, getPedWeapons( client ), getHouseData( houseName, "houseWeapons" ) ) end end end ) Link to comment
[M]ister Posted November 17, 2019 Share Posted November 17, 2019 E como ficou o conteúdo do json quando salvou ? Link to comment
main Posted November 17, 2019 Author Share Posted November 17, 2019 [ { "31": "4522", "29": "3000", "32": "1500" } ] Link to comment
[M]ister Posted November 17, 2019 Share Posted November 17, 2019 O ID que tentou remover era algum destes: 31,32,29 ? Link to comment
[M]ister Posted November 17, 2019 Share Posted November 17, 2019 Tente: itens [ tostring(weaponID) ] = nil Se não resolver, tente depurar o código... exiba os itens, verifica se tal index/valor existe na tabela antes de remover, etc. 1 Link to comment
main Posted November 17, 2019 Author Share Posted November 17, 2019 @MaligNos deu certo convertendo para string, muito obrigado! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now