Codex Studios
Visit our Tebex StoreOur Discord
  • πŸ‘‹Welcome to Codex Studios
  • Overview
    • πŸ’‘About Us
    • ✨Our Features
  • πŸ’…RedM-Scripts
    • 🌐[Codex Studios] : Core
      • Client Side
      • Server Side - CodexCore API
      • Config
      • πŸ“ Client Side (clientside.lua)
      • 🧾 Server Side (serverside.lua)
      • πŸ›‘οΈCodexCore Security Module
      • πŸ—’οΈSecurity Detailed Explanation
      • πŸ”Client-Side: Securing Event Triggers
      • πŸ”’Server-Side: Securing Event Triggers
    • πŸ’ͺ[Codex Studios] : Trust Level
      • Client Side
      • Config
      • SQL
  • πŸ“¦[Codex Studios]: Supplies System
    • Client Side
    • Server-Side Town Supplies
    • Config
    • πŸͺ›Examples and Commands
    • Sql Updated
Powered by GitBook
On this page

Was this helpful?

  1. [Codex Studios]: Supplies System

Client Side

πŸ§‘β€πŸ’» Accessing Town Supplies with CodexStudiosGetTownSupplies

The CodexStudiosGetTownSupplies function allows developers to retrieve information about the supplies available in various towns. This data is made available through an export, enabling access from other scripts in your RedM server. By calling this function, developers can get structured data about towns and their associated supplie

1. Defining the Function

  • CodexStudiosGetTownSupplies():

    • Initializes an empty table suppliesData to store the final output.

    • Checks if the global table supplies_data is a valid table before iterating through it.

    • Iterates through each item in supplies_data, extracting the town name and supplies, then stores them in the suppliesData table.

    • If supplies_data is not a valid table, it prints an error message: "Error: supplies_data is not a table".

2. Exporting the Function

  • The function is exported using exports('CodexStudiosGetTownSupplies', CodexStudiosGetTownSupplies), which makes it accessible from other scripts. This allows other scripts to call this function and retrieve supplies data.

πŸ“š Correct Usage Example

-- Access the function from CodexCore
local townSupplies = exports['codex_supplies']:CodexStudiosGetTownSupplies()

-- Now we can iterate through and print town supplies
for _, town in ipairs(townSupplies) do
    print("Town: " .. town.town)
    print("Supplies: " .. town.supplies)
end

Explanation:

  • exports['CodexCore']:CodexStudiosGetTownSupplies():

    • This exports function is called to retrieve the suppliesData table.

    • The result will be a list of towns and their corresponding supplies.

  • Iterating and Printing:

    • The script iterates through the townSupplies table and prints each town's name and supplies.

🚫 Incorrect Usage Example

Here’s an example of incorrect usage, which may lead to issues:

-- Attempting to call the function without the correct export name
local townSupplies = exports['WrongResourceName']:CodexStudiosGetTownSupplies()

-- If supplies_data is not a valid table, it would print the error message
for _, town in ipairs(townSupplies) do
    print("Town: " .. town.town)
    print("Supplies: " .. town.supplies)
end

Why This Is Incorrect:

  • The export is being called from WrongResourceName, which is not the name of the resource that provides the CodexStudiosGetTownSupplies function. This will cause an error, as it cannot find the function from the wrong resource.

  • Additionally, if the supplies_data is not a valid table or if it’s missing, the suppliesData table returned will be empty, leading to no results in the iteration.

πŸ› οΈ Things to Check for Proper Usage:

  • Export Name: Ensure you're calling the correct export, using the correct resource name and correct function name (CodexStudiosGetTownSupplies).

  • Valid Data: Make sure that supplies_data is a properly defined table before using it. If it's nil or incorrectly structured, the function will return an empty table and print an error message.

πŸ“ Tips for Developers:

  • Error Handling: Always handle situations where the data might be invalid. For example, checking if the result from CodexStudiosGetTownSupplies is empty before attempting to use it in your logic can prevent errors:

    if #townSupplies > 0 then
        for _, town in ipairs(townSupplies) do
            -- Process supplies data
        end
    else
        print("No supplies data available.")
    end
  • Optimize Performance: If you're working with large datasets, consider caching the supplies data locally to avoid redundant function calls, especially in scripts that run frequently.

Previous[Codex Studios]: Supplies SystemNextServer-Side Town Supplies

Last updated 1 month ago

Was this helpful?

πŸ“¦