How weapons work: names, hashes and giving them
A weapon is a name such as WEAPON_PISTOL. The game hashes that name into a number, and natives work with the number. On a framework server the inventory, not the game, records who owns which weapon.
| Weapon | Name | Hash (hex) | Hash (signed) |
|---|---|---|---|
| Pistol | WEAPON_PISTOL | 0x1B06D571 | 453432689 |
| Combat Pistol | WEAPON_COMBATPISTOL | 0x5EF9FEC4 | 1593441988 |
| SMG | WEAPON_SMG | 0x2BE6766B | 736523883 |
| Assault Rifle | WEAPON_ASSAULTRIFLE | 0xBFEFFF6D | -1074790547 |
| Carbine Rifle | WEAPON_CARBINERIFLE | 0x83BF0278 | -2084633992 |
| Pump Shotgun | WEAPON_PUMPSHOTGUN | 0x1D073A89 | 487013001 |
| Sniper Rifle | WEAPON_SNIPERRIFLE | 0x05FC3C11 | 100416529 |
| Knife | WEAPON_KNIFE | 0x99B507EA | -1716189206 |
Each hash is joaat of the lowercase name. A native can hand back the signed form, so compare against a backtick hash rather than a typed number.
The hex and signed columns hold the same 32 bits. The hash is an unsigned 32-bit integer and Lua prints integers as signed, so any value above 2,147,483,647 shows up negative. The whole stock table, with model names and ammo types, is on the weapon hashes reference.
local a = `WEAPON_PISTOL` -- computed at load time, free in loops
local b = GetHashKey('WEAPON_PISTOL') -- same number, computed every call
if GetSelectedPedWeapon(PlayerPedId()) == `WEAPON_PISTOL` then
print('holding a pistol')
endA backtick hash is resolved once, when the script loads, so it costs nothing inside a loop. GetHashKey returns the same number but works it out on every call.
| Server type | Give a weapon with |
|---|---|
| ox_inventory (ESX, Qbox, ox_core, ND_Core) | exports.ox_inventory:AddItem(source, 'WEAPON_PISTOL', 1) on the server |
| QBCore + qb-inventory | Player.Functions.AddItem('weapon_pistol', 1) or the qb-inventory export |
| ESX default loadout | xPlayer.addWeapon('WEAPON_PISTOL', 50) |
| Standalone | GiveWeaponToPed(ped, hash, ammo, false, true) |