Jobs, gangs and items · Lesson 3/3 · 10 min

Items: give, take and make them usable

Items are defined in shared/items, moved on the server and made usable with CreateUseableItem.

In QBCore items exist first as a definition (in qb-core/shared/items.lua) and are then moved between inventories. Like everything sensitive, giving and taking items is done on the SERVER.

Give and take items

lua
local Player = QBCore.Functions.GetPlayer(source)

-- Give 1 'water_bottle' (classic QBCore)
Player.Functions.AddItem('water_bottle', 1)
TriggerClientEvent('inventory:client:ItemBox', source, QBCore.Shared.Items['water_bottle'], 'add')

-- Take 1
Player.Functions.RemoveItem('water_bottle', 1)
TriggerClientEvent('inventory:client:ItemBox', source, QBCore.Shared.Items['water_bottle'], 'remove')

Give / take items

If your server uses modern qb-inventory or ox_inventory, it's done with exports (exports['qb-inventory']:AddItem(src, item, amount) or exports.ox_inventory:AddItem(src, item, amount)). Check which inventory your server runs; the concept (server decides) is the same.

Make an item usable

lua
-- When the player USES the item from the inventory
QBCore.Functions.CreateUseableItem('water_bottle', function(source, item)
  local Player = QBCore.Functions.GetPlayer(source)
  if not Player then return end
  Player.Functions.RemoveItem('water_bottle', 1)
  Player.Functions.SetMetaData('thirst', math.min(100, Player.PlayerData.metadata['thirst'] + 25))
  QBCore.Functions.Notify(source, 'You drank the water', 'success')
end)

CreateUseableItem

CreateUseableItem registers what happens when the item is USED. The item must exist in shared/items.lua and have useable = true. The logic (consume, apply effect) goes on the server.

Practice what you learned

0/3
Test

¿Con qué función le das 1 unidad de 'water' a un jugador en QBCore clásico?

Rellena los huecos

Completa para hacer que el item 'water' sea usable desde el inventario.

1QBCore.Functions.('water', function(source, item)
2 local Player = QBCore.Functions.GetPlayer(source)
3 if not Player then return end
4 Player.Functions.RemoveItem('water', 1)
5end)
Pista

Registra qué pasa al USAR el item; el item debe existir en shared/items.lua con useable = true.

Corrige el error

Al dar el item no se avisa al inventario del cliente, así que no aparece la cajita (ItemBox). Añade el TriggerClientEvent que la muestra.

Este código tiene un fallo:

1local Player = QBCore.Functions.GetPlayer(source)
2if not Player then return end
3Player.Functions.AddItem('water', 1)
4-- falta avisar al cliente

Reescríbelo corregido:

Pista

El evento es 'inventory:client:ItemBox' y le pasas source, la info del item de QBCore.Shared.Items y 'add'.

Challenge: code it yourself

Make a 'sandwich' item that, when used, raises hunger by 30 points and is consumed (RemoveItem).

Write it yourself in your editor (VS Code) and test it on your server. You learn here by doing it, not by copying.

See hint

RemoveItem('sandwich',1) + SetMetaData('hunger', math.min(100, current+30)).

Escribe aquí tu solución:

How was this lesson?