Items

config/events.lua > Config.OnUseEvents

In this little guide I'll show you how to trigger a specific event when the player uses an item crafted in av_business.

Trigger a custom event

  • For this example we are gonna add a custom event for an item type named 'drugs', new item types can be added in config/items.lua > Config.ItemTypes.

  • We are gonna add a new entry to Config.OnUseEvents table:

Config.OnUseEvents = { 
    -- event: Client side event triggered when a player consumes an item from this script
    -- remove: remove item on use?
    ['drink'] = {event = "av_business:consumable", remove = true}, -- this event is on client/editable/items.lua
    ['food'] = {event = "av_business:consumable", remove = true}, -- this event is on client/editable/items.lua
    ['joint'] = {event = "av_business:consumable", remove = true}, -- this event is on client/editable/items.lua
    ['alcohol'] = {event = "av_business:consumable", remove = true}, -- this event is on client/editable/items.lua
    ['box'] = {event = "av_business:box", remove = false}, -- this event is on client/editable/items.lua
    ['drugs'] = {event = "av_business:drugs", remove = true}, -- this event is on client/editable/items.lua
}

['index_key'] = needs to match the item type value,

event = is the event to trigger when you use this item, the event should be client side.

remove = can be true/false, depends if you want to remove the item from player on use

  • The event to trigger can be located in any resource that you want but for this example we are gonna create it in av_business/client/editable/items.lua.

  • We are gonna add our custom event at the end of the file:

RegisterNetEvent("av_business:drugs", function(metadata)
    local type = metadata['type']
    local ingredients = metadata['ingredients']
    local prop = metadata['prop']
    print(type, json.encode(ingredients), prop) -- I'm adding a print to see the info in F8 console, remove it in live server
    -- From here you can trigger an export or event based on item type, ingredients or the prop
    -- If you want to trigger an animation from Config.Animations you can use this:
    -- doAnimation function is in client/editable/animations.lua
    local completed = doAnimation(prop)
    if completed then -- means the progress bar was completed and not canceled at some point
        -- trigger your custom effect, event, export, etc...
    else
        -- do nothing?
    end
end)
  • As you can see the event receives an argument named metadata this info is stored in the item and it contains the item type, ingredients and the prop.

  • Using this info is on you to trigger an export, event or any function you want.

Last updated