Important Functions and Variables
This section details the main functions and variables used or exposed by the script.
Understanding these elements will help you customize or integrate the script with other resources.
Main functions
Section titled “Main functions”In order to set up the ammunition system for the stun dart rifle, you must implement the function below which will verify that the player has ammunition before allowing them to fire.
Checking ammunition possession (server side)
Section titled “Checking ammunition possession (server side)”The function is located in the folder: /server/opensource.lua.
AddEventHandler(Config.EventHasItem, function(target, cb) -- if true -- Remove one ammunition item from inventory -- cb(true)
-- if false -- cb(false)end)Implementation example with ox_inventory
Section titled “Implementation example with ox_inventory”Here is an implementation example using ox_inventory:
AddEventHandler(Config.EventHasItem, function(target, cb) local src = target or source
-- Check if the player has at least 1 ammunition local count = exports.ox_inventory:Search(src, 'count', 'ammo-dartrifle')
if count and count > 0 then -- Remove 1 ammunition local removed = exports.ox_inventory:RemoveItem(src, 'ammo-dartrifle', 1) if removed then cb(true) else cb(false) end else cb(false) endend)In the weapons.lua file, add the item definitions for the stun dart rifle and its ammunition:
['WEAPON_DARTRIFLE'] = { label = 'Stun dart rifle', weight = 4200, durability = 0.5, ammoname = 'ammo-dartrifle', client = { image = 'dart_rifle.png', } },In the items.lua file, add the item definition for stun dart rifle ammunition:
["ammo-dartrifle"] = { label = 'Stun dart rifle ammunition', weight = 20, stack = true, description = 'Dart for stun dart rifle.', client = { image = 'dart_rifle_ammo.png', } },Add the dart_rifle.png and dart_rifle_ammo.png images to the web/images folder of ox_inventory.
That’s it! You now have a functional ammunition system for the stun dart rifle.
Best practices
Section titled “Best practices”- Use local variables (
local) to limit the scope to the concerned function or script.