[QUESTION/HELP] Make a config file (LUA)

Hello All,

Im trying to make a resource that handles a lot of scripts. Therefore rather then refining a variable in every file ive decided to make a config. What it looks like is elow. However, when I use the variable in a script (in the same resource) it doesn’t seem to work.

Example follows:

Config File:

local var = {}

var.ped = GetPlayerPed(-1)

return var

Use in script:

local ped = var.ped

coords = GetEntityCoords(var.ped)

Any ideas to make this work?

Thanks
Harry

Don’t put this in the config as the player ped changes often for example when someone respawns

ok what if there was another example like:

local var = {}
var.life_relif = 0
return var

and then I used it here:

function textRelif(content) 
    SetTextFont(1)
    SetTextProportional(0)
    SetTextScale(0.5,0.5)
    SetTextEntry("STRING")
    AddTextComponentString("Relif: " ..content.."%")
    DrawText(0.88,0.71)
end
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(2)
        local Relif = var.life.relif
        textRelif(math.floor(Relif))
    end
end)

Hey @HarryCatraz,
I’m not really used to code in .lua but you should create the config file, as you said. Don’t declare the variables with “local”. On the script file you should start your code with require "config.lua".

As I said, I don’t have a lot of experience with lua, anyways I’ll try to help you with my experience on other languages. Look to the require filename.lua as an imported library of any other language (e.g. in C you use: #include <stdio.h>)

config.lua
var = {}
var.ped = GetPlayerPed(-1)
return var
script.lua
require "config.lua"
function getCoords()
     local ped = var.ped
     coords = GetEntityCoords(var.ped)
end

Hope this helps you out :wink:

I search a little and I found that lua devs use the prefix _G but you can only omit the local and it should be ok.
e.g. _G.var

I also found two useful links that can help you:


So before I started using C# I was using config files like this for Lua (otherwise I used JSON files)

__resource.lua

resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

---------------------------------------------------------------------------
-- Configs
---------------------------------------------------------------------------
server_script "configs/base.lua"
server_script "configs/testing.lua"

---------------------------------------------------------------------------
-- Server
---------------------------------------------------------------------------
server_script "server.lua"

---------------------------------------------------------------------------
-- Client
---------------------------------------------------------------------------
client_script "client.lua"

configs/base.lua

Config = {}

Config.Connection = {

isWhitelisted = false

}

configs/testing.lua

Config.Testing = {
	models = {"model_one", "model_two"}
}

server.lua

print(json.encode(Config.Testing["models"]))

client.lua
Nothing.. As the configs in this example are server sided

IMAGES
https://i.gyazo.com/0654e78d72ffd34b6156c736355341da.png
https://i.gyazo.com/632be1e2c375b32d405fae6aed97c5ca.png

Since Config was defined as a global var in the base.lua you can keep appending tables to it from other Lua config files so you can keep config files smaller and more broken up.

Now that I have been using C# I have been using JSON which is available to use in Lua.

Ok,

so now ive got this. However, Ive noticed you are defining the vars on the server side. Can I do it client sided aswell?

local Config = {}     

Config.General = {
    cfg.paycheckDelay = 300 --5 Minutes
    cfg.hungerRate = 120 --2 Minutes
    cfg.thirstRate = 120 --2 Minutes
    cfg.relifRate = 120 --2 Minutes
}

Config.Var = {    
    var.life_money = 0
    var.life_bank = 0
    var.life_hunger = 0
    var.life_thirst = 0
    var.life_relif = 0
    var.life_paycheck = 5000   
    var.life_threatlevel = "Green"
    var.life_recentmajorcrime = false 
    var.cop_isonduty = false
    var.medic_isonduty = false
}

Config.Stringtable = {
    @STR_Example_Test = "Example String"
    @STR_Shops_Clothing = "Clothing Store"
    @STR_Shops_Gang = "Gang Clothing Store"
    @STR_Shops_Diving = "Diving Store"
    @STR_Shops_Weapon = "Weapon Store"
    @STR_Shops_Market = "Market"
    @STR_Shops_General = "General Store"
    @STR_Shops_FishMarket = "Fish Market"
    @STR_Shops_Coffee = "Coffee Shop"
    @STR_Shops_DrugDealer = "Drug Dealer"
    @STR_Shops_Oil = "Oil Trader"
    @STR_Shops_Glass = "Glass Trader"
    @STR_Shops_Cement = "Cement Shop"
}

--[[ Config.VehShops = {
    {model=vehiclename, buyprice, sellprice, insuranceprice, faction},
    {model=adder, 1000000, 500000, 5000, civilian},
    {model=firetruck, 10000, 5000, 500, ems},
    {model=police, 10000, 5000, 500, cop}
}

Config.VehShopLocations = {
    {title="marker name", colour=1, id=361, x=X,   y=Y,  z=Z},
    {title="Gas Station", colour=1, id=361, x=49.4187,   y=2778.793,  z=58.043},
} ]]

Testing Now

Yes. In the __resource.lua load the configs as a client script as I did for the server script.

OR

Pass the configs over an event amd store them.

Made this config using Cops_FiveM as a base.

Now got this:

Config = {

    General = {
        paycheckDelay = 300, --5 Minutes
        hungerRate = 120, --2 Minutes
        thirstRate = 120, --2 Minutes
        relifRate = 120 --2 Minutes
    },

    Var = {    
        life_money = 0,
        life_bank = 0,
        life_hunger = 0,
        life_thirst = 0,
        life_relif = 0,
        life_paycheck = 5000,
        life_threatlevel = "Green",
        life_recentmajorcrime = false,
        cop_isonduty = false,
        medic_isonduty = false
    },

    STR = {
        Example_Test = "Example String",
        Shops_Clothing = "Clothing Store",
        Shops_Gang = "Gang Clothing Store",
        Shops_Diving = "Diving Store",
        Shops_Weapon = "Weapon Store",
        Shops_Market = "General Market",
        Shops_FishMarket = "Fish Market",
        Shops_Coffee = "Coffee Shop",
        Shops_DrugDealer = "Drug Dealer",
        Shops_Oil = "Oil Trader",
        Shops_Glass = "Glass Trader",
        Shops_Cement = "Cement Shop"
    },

    VehShops = {
        {model=vehiclename, buyprice, sellprice, insuranceprice, faction},
        {model=adder, 1000000, 500000, 5000, civilian},
        {model=firetruck, 10000, 5000, 500, ems},
        {model=police, 10000, 5000, 500, cop}
    },

    VehShopLocations = {
        {title="marker name", colour=1, id=361, x=X,   y=Y,  z=Z},
        {title="Gas Station", colour=1, id=361, x=49.4187,   y=2778.793,  z=58.043},
    }

}     

Use in script

function textHealth(content) 
    SetTextFont(1)
    SetTextProportional(0)
    SetTextScale(0.5,0.5)
    SetTextEntry("STRING")
    AddTextComponentString("Health: " ..content.."%")
    DrawText(0.88,0.62)
end
function textHunger(content) 
    SetTextFont(1)
    SetTextProportional(0)
    SetTextScale(0.5,0.5)
    SetTextEntry("STRING")
    AddTextComponentString("Hunger: " ..content.."%")
    DrawText(0.88,0.65)
end
function textThirst(content) 
    SetTextFont(1)
    SetTextProportional(0)
    SetTextScale(0.5,0.5)
    SetTextEntry("STRING")
    AddTextComponentString("Thirst: " ..content.."%")
    DrawText(0.88,0.68)
end
function textRelif(content) 
    SetTextFont(1)
    SetTextProportional(0)
    SetTextScale(0.5,0.5)
    SetTextEntry("STRING")
    AddTextComponentString("Relif: " ..content.."%")
    DrawText(0.88,0.71)
end
function textStamina(content) 
    SetTextFont(1)
    SetTextProportional(0)
    SetTextScale(0.5,0.5)
    SetTextEntry("STRING")
    AddTextComponentString("Stamina: " ..content.."%")
    DrawText(0.88,0.74)
end
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(2)
        local Health = GetEntityHealth(GetPlayerPed(-1))
        local HealthNew = Health - 100
        textHealth(math.floor(HealthNew))
        textHunger(math.floor(Config.Var.life_hunger))
        textThirst(math.floor(Config.Var.life_thirst))
        textRelif(math.floor(Config.Var.life_relif))
        textStamina(math.floor(GetPlayerSprintStaminaRemaining(GetPlayerPed(-1))))
    end
end)

Error on debug console:

Error resuming coroutine: @Catraz_RP/client/functions/fbc_RPHUD.lua:47: Attempt to call a nil value (global "Config")

Any ideas why?

What’s the difference?

forget it my fault. Thought there would be a difference, because of the when you use

config {
      counter = 0
}

there is no . for the config like in this example:

config{}
     config.counter = 0

I guess the . is “automaticly” added when your using the first variant.