Esc to clear, Enter to open the best match, click an export to open it in its page
whip_gunny

Weapon System

whip_gunny is WHIP's weapon system: guns, magazines, ammunition, melee weapons, throwables and the jerry can. It ships with WHIP and needs no setup: its items are in /whip → Economy → Items, ready to sell or hand out.

A resource that calls it adds dependency 'whip_gunny'. For gunny as a worked example, see Learning from gunny.

How it works

Weapons are inventory items with state, kept in whip_core's item metadata: gunny owns the rules, whip_core owns the data. A revolver or pump shotgun carries its rounds itself.

gun item ─ metadata ─┬─ serial          "AB12CD34EF56"
                     ├─ attachments     { muzzle = 'gunny_suppressor' }
                     ├─ attachmentMeta  { muzzle = { ... } }
                     └─ magazine ─┬─ item    'gunny_mag_rifle'
                                  ├─ serial  'GH78IJ90KL12'
                                  ├─ ammo    24
                                  └─ ...     the rest of the magazine's metadata (its uid)

Magazines and calibers

Controls

KeyControllerAction
RBReload, on foot only
GLB + BCheck the ammo; with sticky bombs out, set them all off
E at the benchAUse the weapon bench

Players can rebind R and G, and move the controller buttons in the pause menu. In a seat, reloading is automatic (the last round, or the trigger on an empty gun) and takes at least the weapon's reloadTime. On a controller, double-tap RB to start or stop aiming, then hold it to fire.

The magazine slot, throwables and the jerry can

The weapon benchServer owners#

Attachments go on and come off at the weapon bench: E at it with a gun in hand. Each attachment is an item, and each weapon lists what it takes slot by slot (scope, muzzle, barrel, grip, flashlight) as GTA components in items/attachments.lua. The bench's place, model and label are Config.WeaponBench in gunny's config.lua; enabled = false removes it.

Adding weaponsServer owners#

To add guns without editing gunny's files, so updates never touch them, use a weapon pack.

From your resource

A gun is an ordinary item: AddItem gives one empty, RemoveItem takes one, and GiveWeapon adds a magazine, rounds or your own serial. The held gun is GetHeldItem(source) on the server and GetDrawnWeapon on the client.

Give a gun, and take that one back#

For an armory or a mission's kit, note the serials you hand out and take back those. FindItemByMetadata finds each, and StoreTake takes it (a drawn gun is put away first, its magazine with it). A spare magazine put into a gun is inside it, so its serial finds nothing until it comes out.

local issued = {}   -- [source] = the serials handed out

local function issue(source)
    local ok, where, gunSerial = exports.whip_gunny:GiveWeapon(source, 'gunny_carbinerifle', { loaded = true }, 'armory issue')
    if not ok then return false, where end
    local _, _, magSerial = exports.whip_gunny:GiveMagazine(source, 'gunny_mag_carbinerifle', 999, 'armory issue')
    issued[source] = { gunSerial, magSerial }
    return true
end

local function takeBack(source)   -- exactly what was issued
    for _, serial in ipairs(issued[source] or {}) do
        local at = exports.whip_core:FindItemByMetadata('serial', serial)
        if at and at.kind == 'player' and at.id == source then
            exports.whip_core:StoreTake(at, 1, { reason = 'returned to the armory' })
        end
    end
    issued[source] = nil
end

Every shot fired#

whip_gunny:weapon:fired comes each time the server takes rounds off a gun: automatic fire is counted a frame at a time, so shots can be more than 1. It runs as often as anyone fires, so keep your handler short.

-- Server: a "shots fired" call, at most one a minute per shooter
local lastAlert = {}
AddEventHandler('whip_gunny:weapon:fired', function(e)
    local now = GetGameTimer()
    if e.coords and now - (lastAlert[e.source] or 0) > 60000 then   -- coords is nil once they've gone
        lastAlert[e.source] = now
        AlertPolice(e.coords, e.item)   -- your own
    end
end)

The rounds on your HUD#

The client has no per-shot event: read GetDrawnWeapon a few times a second between whip_gunny:weapon:drawn and :holstered. Its count is live.

local following = 0   -- each draw and holster bumps it, ending the loop before
AddEventHandler('whip_gunny:weapon:drawn', function(w)
    following = following + 1
    local mine = following
    if w.kind ~= 'gun' then return end
    CreateThread(function()
        local gun = exports.whip_gunny:GetDrawnWeapon()
        while following == mine and gun do
            ShowAmmo(gun.ammo, gun.capacity)   -- your own
            Wait(150)
            gun = exports.whip_gunny:GetDrawnWeapon()
        end
    end)
end)
AddEventHandler('whip_gunny:weapon:holstered', function() following = following + 1; HideAmmo() end)

No weapons here

For a no-weapons zone, handcuffs or jail, call BlockHands with only = 'weapons', on the client for a place or the server for one player. gunny refuses a draw with the block's label and puts away a drawn gun until UnblockHands.

exports.whip_core:BlockHands(source, 'myjail', { only = 'weapons', label = 'No weapons in here' })

A zone's own Weapons away rule works on GTA's weapons, not gunny's.

Weapon packs#

A weapon pack adds guns from your own resource without editing gunny. It streams the weapons, ships icons as html/img/items/<item>.webp, and registers each item inside Gunny.register (no dependency needed), magazines first, in a file both sides load. A pack's gun needs what any gun needs (Adding weapons), may use gunny's magazines and calibers, and gets gunny's menus, magazine slot, settings and bench.

-- fxmanifest.lua (models and textures go in stream/)
shared_script '@whip_gunny/lib/pack.lua'
shared_script 'gunny.lua'
data_file 'WEAPONCOMPONENTSINFO_FILE' 'data/mypack_weaponcomponents.meta'   -- before the weapons that list them
data_file 'WEAPON_METADATA_FILE' 'data/mypack_weaponarchetypes.meta'
data_file 'WEAPON_ANIMATIONS_FILE' 'data/mypack_weaponanimations.meta'
data_file 'WEAPONINFO_FILE' 'data/mypack_weapons.meta'
files { 'data/*.meta', 'html/img/items/*.webp' }

-- gunny.lua: runs when gunny is up, and again each time it starts
Gunny.register(function()
    exports.whip_gunny:RegisterMagazine('mypack_mag_m4', {
        weight = 0.2, label = 'M4 Magazine', prop = 'w_ar_mypack_m4_mag', caliber = '5.56x45', capacity = 30,
    })
    exports.whip_gunny:RegisterWeapon('mypack_m4', {
        weight = 3.2, label = 'M4', description = 'A carbine.', hash = `WEAPON_MYPACK_M4`,
        prop = 'w_ar_mypack_m4', caliber = '5.56x45', compatibleMags = { 'mypack_mag_m4' }, reloadTime = 2200,
        attachments = { scope = { medium = `COMPONENT_MYPACK_M4_SCOPE` } },
    })
end)

gunny's own fxmanifest.lua streams its add-on guns the same way and is a full example.

Debugging#

/w → Debug → Gunny Debug shows the drawn weapon's rounds as the game, gunny and the server count them, and the shot reports. /gunnydebugreset zeroes the counters; /gunnydebugserver says what's drawn. [GUNNY] WARNING lines always print: a failed write, or an undone change.

A gun that won't reload#

The player seesWhy, and what to do
No magazines for this weapon on youNone of its compatibleMags is in their pockets
All your magazines for this weapon are emptyLoad Bullets on one, with its caliber's rounds carried
No ammunition for this weapon on youA gun with no magazine needs its caliber's loose rounds
No room for the old magazineThe old one had nowhere to go, not even the ground: the reload was undone
Nothing at allR does nothing in a vehicle, with a melee weapon, with a screen or bar up, or while the gun is being put away