Jump to content

Store gun at vehicle trunk


Recommended Posts

Posted

I am developing a system of storing the player's weapons in the trunk of his vehicle. I would like some help on how I should make the stored weapon variables. Here is the server:

 

function getNearestVehicle( player ) 
    local x, y, z = getElementPosition( player ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance 
            nearestVehicle = v 
        end 
    end 
    return nearestVehicle or false 
end 
  
 
  
function doSomethingWithTheNearestVeh( player ) 
    local vehicle = getNearestVehicle( player )  
    local x1, y1, z1 = getElementPosition ( vehicle ) 
    local x2, y2, z2 = getElementPosition ( player ) 
    if vehicle then 
        if not getPedOccupiedVehicle( player ) then
		--local accName = getAccountName ( getPlayerAccount ( player ) )  
            if getDistanceBetweenPoints3D ( x1, y1, z1, x2, y2, z2 ) < 5 then 
				for slot = 0, 12 do 
				local Armas = getPedWeapon ( player, slot )
				local Municao = getPedTotalAmmo ( player, slot ) 
					if Armas > 0 then 
						if Municao > 0 then  
							weapon_nome = getWeaponNameFromID ( Armas )
							outputChatBox ("#FF0000✘ #ffffffINFO #FF0000✘➺ #ffffff"..weapon_nome.." com "..Municao.." Bala(s) !", player, 255, 255, 255, true ) 
						else
						outputChatBox ( "Você não possui armas para serem guardadas.", player, 125, 0, 0 )
						end											 						 
					end  
				end
            else 
                outputChatBox ( "Não existem veículos próximos.", player, 125, 0, 0 ) 
			end
        else     
            outputChatBox ( "Não existem veículos próximos.", player, 125, 0, 0 ) 
        end 
    end 
end 
addCommandHandler( "portamalas", doSomethingWithTheNearestVeh ) 

 

  • Moderators
Posted (edited)

 

13 hours ago, Nickqq said:

I am developing a system of storing the player's weapons in the trunk of his vehicle

How about you save it first inside of your memory? You can always upgrade it to SQL/MySQL later.

 

Start with something as simple as this.

local vehicleTrunkItems = {}

function saveItemInVehicleTrunk (vehicle, item)
	local trunk = vehicleTrunkItems[vehicle]
	if not trunk then
		trunk = {}
		vehicleTrunkItems[vehicle] = trunk
	end
	trunk[#trunk + 1] = item
end

function getItemsInVehicleTrunk (vehicle)
	return vehicleTrunkItems[vehicle] or false
end

Define all functions you need, till it more or less works as you want.

Edited by IIYAMA
  • Like 1

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted (edited)
10 hours ago, IIYAMA said:

 

How about you save it first inside of your memory? You can always upgrade it to SQL/MySQL later.

 

Start with something as simple as this.


local vehicleTrunkItems = {}

function saveItemInVehicleTrunk (vehicle, item)
	local trunk = vehicleTrunkItems[vehicle]
	if not trunk then
		trunk = {}
		vehicleTrunkItems[vehicle] = trunk
	end
	trunk[#trunk + 1] = item
end

function getItemsInVehicleTrunk (vehicle)
	return vehicleTrunkItems[vehicle] or false
end

Define all functions you need, till it more or less works as you want.

Thanks for the asnwer!! I made it ? thanks.. but now i need to find a way to a cop see the weapons stored at the players car. Can you help me? 

 

There is my script

 

function getNearestVehicle( player ) 
    local x, y, z = getElementPosition( player ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance 
            nearestVehicle = v 
        end 
    end 
    return nearestVehicle or false 
end 
  
local playerWeapons = { } 
  
function doSomethingWithTheNearestVeh( player ) 
    local vehicle = getNearestVehicle( player )  
    local x1, y1, z1 = getElementPosition ( vehicle ) 
    local x2, y2, z2 = getElementPosition ( player ) 
    if vehicle then 
        if not getPedOccupiedVehicle( player ) then
		local accName = getAccountName ( getPlayerAccount ( player ) )  
            if getDistanceBetweenPoints3D ( x1, y1, z1, x2, y2, z2 ) < 5 then 				
				local Guardou = getElementData(player, "GuardouArmas")
				if Guardou == false then
					for slot = 0, 12 do 
					local weapon = getPedWeapon ( player, slot )
					local ammo = getPedTotalAmmo ( player, slot )							
						if weapon > 0 then 
							if ammo > 0 then
								if ( not playerWeapons [ player ] ) then 
									playerWeapons [ player ] = { } 
								end 
								outputDxBox(player, 'Você está guardando a(s) arma(s) no porta-malas.', "info")
								setTimer( outputDxBox, 5000, 1, player, 'Você guardou a(s) arma(s) no porta-malas.', "success")
								playerWeapons [ player ] [ weapon ] = ammo 																		
								weapon_nome = getWeaponNameFromID ( weapon )
								setTimer(outputChatBox, 5000, 1, "#FF0000✘ #ffffffINFO #FF0000✘➺ #ffffff"..weapon_nome.." com "..ammo.." Bala(s) guardado!", player, 255, 255, 255, true ) 
								--takeAllWeapons(player)
								setTimer(takeAllWeapons, 5000, 1, player) 
								setPedAnimation( player, "FLAME", "FLAME_fire" )  
								setTimer( setPedAnimation, 5000, 1, player )
								setElementData(player, "GuardouArmas", true)		
							end											 						 
						end	 
					end
				else
					outputDxBox(player, 'Você já guardou suas armas. /pegararmas para pega-las.', "warning")
				end
            else 
				outputDxBox(player, 'Não existem veículos próximos.', "error")
			end
        else     
            outputDxBox(player, 'Não existem veículos próximos.', "error") 
        end 
    end 
end 
addCommandHandler( "guardararmas", doSomethingWithTheNearestVeh ) 

addEventHandler("onResourceStart", resourceRoot,
function()
	setElementData(root, "GuardouArmas", false)
end)

addEventHandler( "onResourceStop", root,
    function( resource )
        setElementData(root, "GuardouArmas", false)
    end
)

function PegarArmas(player)
    local vehicle = getNearestVehicle( player )  
    local x1, y1, z1 = getElementPosition ( vehicle ) 
    local x2, y2, z2 = getElementPosition ( player ) 
    if vehicle then 
        if not getPedOccupiedVehicle( player ) then
		local accName = getAccountName ( getPlayerAccount ( player ) )  
            if getDistanceBetweenPoints3D ( x1, y1, z1, x2, y2, z2 ) < 5 then	
				local Guardou = getElementData(player, "GuardouArmas")
				if Guardou == true then			
					if ( playerWeapons [ player ] ) then 
						outputDxBox(player, 'Você está retirando a(s) arma(s) do porta-malas!', "info")
						setTimer( outputDxBox, 5000, 1, player, 'Você pegou suas armas no porta-malas.', "success")		
						for weapon, ammo in pairs ( playerWeapons [ player ] ) do 
							giveWeapon ( player, tonumber ( weapon ), tonumber ( ammo ) ) 
							setPedAnimation( player, "FLAME", "FLAME_fire" )  
							setTimer( setPedAnimation, 5000, 1, player ) 
							setTimer(outputChatBox, 5000, 1, "#FF0000✘ #ffffffINFO #FF0000✘➺ #ffffff"..weapon_nome.." com "..ammo.." Bala(s) retirado!", player, 255, 255, 255, true )	
							setElementData(player, "GuardouArmas", false)
						end  
					end 
				else
					outputDxBox(player, 'Você não possui armas guardadas.', "warning")
				end				
            else 
				outputDxBox(player, 'Não existem veículos próximos.', "error")
			end
        else     
            outputDxBox(player, 'Não existem veículos próximos.', "error") 
        end 
    end 				
  
	playerWeapons [ player ] = nil  
end	
addCommandHandler( "pegararmas", PegarArmas )  
  
  
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("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) 
  
function outputDxBox(thePlayer, text, type)
	exports.Scripts_Dxmessages:outputDx(thePlayer, text, type)
end

 

Edited by Nickqq
  • Moderators
Posted
11 hours ago, Nickqq said:

but now i need to find a way to a cop see the weapons stored at the players car. Can you help me? 

Where in the script are you storing the weapons in the vehicle?

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted (edited)
1 hour ago, IIYAMA said:

Where in the script are you storing the weapons in the vehicle?

That was as close as I could get to making it work =/ do you have any recommendation?

 

Maybe 

 

local vehicleTrunkItems = {}

function saveItemInVehicleTrunk (vehicle, item)
	local trunk = vehicleTrunkItems[vehicle]
	if not trunk then
		trunk = {}
		vehicleTrunkItems[vehicle] = trunk
	end
    for i=0,12 do 
        local weapon = getPedWeapon(source, i) 
        local ammo = getPedTotalAmmo(source, i) 
		trunk[#trunk + 1] = weapon
		trunk[#trunk + 1] = ammo
        --setAccountData(account,"w"..tonumber(i), weapon) 
        --setAccountData(account,"a"..tonumber(i), ammo) 
    end 	
	trunk[#trunk + 1] = item
	takeAllWeapons(source)
end

??

Edited by Nickqq
  • Moderators
Posted
16 minutes ago, Nickqq said:

That was as close as I could get to making it work =/ do you have any recommendation?

How about you start with using the code I gave you? And change it to something that could work for you?

 

 

-- test data
local weapon = 31
local ammo = 3000
---

local item1 = {type = "weapon" , weapon = weapon, ammo = ammo}

saveItemInVehicleTrunk (vehicle, item1)



local item2 = {type = "weapon" , weapon = 38, ammo = 9999} -- minigun

saveItemInVehicleTrunk (vehicle, item2)

 

 

local items = getItemsInVehicleTrunk (vehicle)

for i=1, #items do
	local item = items[i]
	if item.type == "weapon" then
		iprint("weapon id:", item.weapon, ", ammo:", item.ammo)
	end
end

 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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