How to Create Money as Item in QBCore

money as item

Introduction:
In QBCore, you have the flexibility to create custom money items, allowing for unique in-game currencies or monetary systems. This tutorial will guide you through the process of adding a custom money item to your QBCore server. To achieve this, we’ll be modifying the code in the qb-core/server/player.lua file.

Prerequisites:

  • A working QBCore server installation.
  • Basic knowledge of Lua programming.
  • Access to your server’s files, specifically the player.lua file.

Step 1: Locate the Player.lua File
Navigate to your QBCore server’s file structure and find the player.lua file within the qb-core/server directory. This is where we’ll make the necessary modifications to add a custom money item.

Step 2: Understanding the Code
Before we proceed, let’s understand the provided code. In the code you’ve provided, there are three main functions: AddMoney, RemoveMoney, and SetMoney. These functions handle different aspects of managing money, including adding, removing, and setting the player’s money.

Step 3: Define Your Custom Money Item
To create a custom money item, you’ll first need to decide on its name and attributes. In this example, let’s create a custom money item named “custom_money.” You can choose a different name if you prefer.

Step 4: Add the Money Item to the Player’s Inventory
Modify the AddMoney function to include the logic for adding the custom money item to the player’s inventory. Add the following code to the function:

if moneytype == 'cash' then
    self.Functions.AddItem('cash', amount)
elseif moneytype == 'custom_money' then
    self.Functions.AddItem('custom_money', amount)
end

This code checks if the moneytype is “custom_money” and adds the specified amount to the player’s inventory.

Step 5: Remove the Money Item from the Player’s Inventory
To enable the removal of the custom money item, modify the RemoveMoney function. Add the following code:

if moneytype == 'cash' then
    if self.Functions.GetItemByName('cash') ~= nil then
        if self.Functions.GetItemByName('cash').amount >= amount then
            self.Functions.RemoveItem('cash', amount)
        else
            return false
        end
    else
        return false
    end
elseif moneytype == 'custom_money' then
    if self.Functions.GetItemByName('custom_money') ~= nil then
        if self.Functions.GetItemByName('custom_money').amount >= amount then
            self.Functions.RemoveItem('custom_money', amount)
        else
            return false
        end
    else
        return false
    end
else
    -- Handle other money types here
end

This code allows the removal of the custom money item from the player’s inventory when specified.

Step 6: Set the Custom Money Item
Finally, modify the SetMoney function to include the custom money item. Add the following code:

if moneytype == 'cash' then
    if self.Functions.GetItemByName('cash') ~= nil then
        local currentCashAmount = self.Functions.GetItemByName('cash').amount
        self.Functions.RemoveItem('cash', currentCashAmount)
        self.Functions.AddItem('cash', amount)
    else
        self.Functions.AddItem('cash', amount)
    end
elseif moneytype == 'custom_money' then
    if self.Functions.GetItemByName('custom_money') ~= nil then
        local currentCustomMoneyAmount = self.Functions.GetItemByName('custom_money').amount
        self.Functions.RemoveItem('custom_money', currentCustomMoneyAmount)
        self.Functions.AddItem('custom_money', amount)
    else
        self.Functions.AddItem('custom_money', amount)
    end
else
    -- Handle other money types here
end

This code ensures that the custom money item is correctly set in the player’s inventory.

Step 7: Save and Test
Save the player.lua file with your modifications. Now, when you use these functions with “custom_money” as the moneytype, they will interact with your custom money item.

Conclusion:
You have successfully added a custom money item to your QBCore server. This allows you to create unique in-game currencies or monetary systems tailored to your server’s needs. You can further expand on this concept by customizing the item’s name, icon, and other attributes to make it more immersive for your players.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

pt_BRPortuguês do Brasil