QBCore in depth: the player and their money · Lesson 2/4 · 10 min

The Player object and its money

On the server, Player represents the player. With it you read and move their money safely.

On the server everything revolves around the Player object: you get it from the source (its server ID). Inside is PlayerData (data) and Functions (actions).

lua
local QBCore = exports['qb-core']:GetCoreObject()

local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end -- always check (it might not be loaded)

Get the player

Read and move money

lua
-- QBCore has three money types by default: 'cash', 'bank' and 'crypto'
local cash = Player.PlayerData.money['cash']
local bank = Player.PlayerData.money['bank']

-- Also with the function (safer):
local efectivo = Player.Functions.GetMoney('cash')

-- Move money (the 3rd argument is the reason, for the logs)
Player.Functions.AddMoney('cash', 500, 'venta-pescado')
Player.Functions.RemoveMoney('bank', 200, 'compra-tienda')

Money with QBCore

ALL of this goes on the SERVER. Money is moved where the authority is. Never give money from the client. RemoveMoney returns true/false depending on whether the player had enough: use it to validate.

Example: charging while validating they have a balance

lua
RegisterNetEvent('tienda:server:comprar', function()
  local src = source
  local Player = QBCore.Functions.GetPlayer(src)
  if not Player then return end

  local precio = 1500
  if Player.Functions.RemoveMoney('bank', precio, 'compra-tienda') then
    -- RemoveMoney returned true: they had the balance and were charged
    QBCore.Functions.Notify(src, 'Purchase completed', 'success')
  else
    QBCore.Functions.Notify(src, 'You don\'t have enough money', 'error')
  end
end)

Validated charge

Practice what you learned

0/3
Rellena los huecos

Obtén el jugador en el servidor a partir de su source y comprueba que existe.

1local Player = QBCore.Functions.(source)
2if not Player then end
Pista

El jugador puede no estar cargado todavía; por eso siempre el guard justo después.

Test

¿Cómo lees el efectivo ('cash') de un jugador desde su PlayerData?

Corrige el error

Este cobro no valida si el jugador tiene saldo. Usa el valor que devuelve RemoveMoney para decidir.

Este código tiene un fallo:

1local Player = QBCore.Functions.GetPlayer(source)
2if not Player then return end
3Player.Functions.RemoveMoney('bank', 1500, 'compra')
4QBCore.Functions.Notify(source, 'Compra realizada', 'success')

Reescríbelo corregido:

Pista

RemoveMoney devuelve true/false según el saldo. Envuelve el cobro en un if y notifica el error si devuelve false.

Challenge: code it yourself

Make a server event that charges 250 from the player's bank and only shows a success message if RemoveMoney returned true.

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

Use if Player.Functions.RemoveMoney('bank', 250, 'reason') then ... else ... end.

Escribe aquí tu solución:

How was this lesson?