MainSCR

Dúvida toJSON

Recommended Posts

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 by MainSCR

Share this post


Link to post

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
)

 

  • Thanks 1

Share this post


Link to post

@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
)

 

Share this post


Link to post

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.

  • Thanks 1

Share this post


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.