How are server.lua files read/executed by lua?

In my first attempt at writing my own script from scratch, there is one thing I can’t seem to figure out. As the topic title says, how is code from server files read/executed?

The top of my server script looks like this:

-- 
-- If we grab Config values up here, does server keep them in memory, making them safe to use?
-- 
local trucks = Config.Trucks
local boxTrucks = Config.BoxTrucks
local dumpTrucks = Config.DumpTrucks
local fluidTrucks = Config.FluidTrucks
local truckData = Config.TruckData
local poiData = Config.POI
local cargoData = Config.CargoTypes

print('Initiated config values') 

As you can see I’m trying to save some tables from the config file to local variables. Some of these tables do actually seem to be filled with data from the config, and others stay nil. It also seems like at different places in the script, some might be filled that weren’t earlier on in the script.

One solution I have found searching the internet was to make sure that the config file is loaded first in fxmanifest, which it is, I put it at the very top.

shared_scripts {
    'config.lua',
    '@someplugin/client.lua' -- calling a client script from server side here, is that ok?
    
}

server_scripts {
    --'config.lua',    also tried putting it here
    '@oxmysql/lib/MySQL.lua',
    'server.lua'

}

client_scripts {
    --'config.lua',    and here
    '@someplugin/client.lua',
    '@someplugin/client.lua',
    'client.lua'
}

And just for being complete, my config file really does contain all the tables mentioned:

Config = {}

Config.Trucks = { 'scrap' }

Config.BoxTrucks = { 'scrap' }

Config.DumpTrucks = { 'scrap' }

Config.FluidTrucks = { 'scrap' }

Config.TruckData = {
    ['scrap'] = {
        modelName = 'scrap',
    }
}

Config.POI = {
    ['Beachfront Market'] = { 
        availableCargo = { 'Sand' }, -- things you can pick up here
        acceptingCargo = { 'Old Junk' } -- things you can drop off here
    }
}

Config.CargoTypes = {
    ['Old Junk'] = {
        ValuePerKg = 0.2
    }
}

This is what brings me to my question and makes me wonder how the server side files are actually being read/executed. Any insight on this would be great, thanks for reading!

1 Like

Usually the order in the fxmanifest equals the loadorder of the files. However I also found that using shared_script first doesn’t always mean that those files are loaded “first”.

Tbh I only use shared_script for standalone parts of scripts that can also be loaded in other scripts.

But I also ran in the same problem when I did it like you did while experimenting with a new file structure for my newer releases.

E.g. if you put the config in server_ and client_scripts it will work as expected.

Thanks alot for your response!

I have tried putting the config in both server and client scripts instead, but the result was the same sadly.

While I cannot help you with the file load order (but if I remember, shared scripts are loaded after client/server scripts), keep in mind that in lua, saving array as other variables doesn’t duplicate the array.

Which means that if you modify the contents of trucks, you will also modify Config.Trucks (on the server-side, ofc), because you’re not saving the array itself, but the tbl: id of the array that is saved in memory.

Also shared scripts, while they can be accessed by both the server and the client, they are not the same file. Changing variables server-side on a shared script won’t duplicate on the client.

Are you sure the file is loaded by fxmanifest.lua? Can you put the print at the start of the file (first line) instead? Can you share the file structure of your resource?

Thanks alot for the info! I have been wondering how to best use data from my config files (which I do use as static data files, I don’t want to change the config tables) in my server side script. I assume that, because a config file is also accessible by the client, they are prone to abuse. From your information I’m getting the idea that the only safe way to use this data on the server side, would be to put it in a database instead of a config file. If you have any other advice on the topic I’d love to hear it.

I did try putting the print on the first line of the file, it did not make a difference. I am very sure the file is loaded by fxmanifest, as there is lots of code in there that does work properly. I am not 100% sure on what you mean by file structure, but I simply have a folder with my config.lua, server.lua, client.lua and fxmanifest.lua in it. (no subfolders or anything)

/edit
I managed to find the print statement from the top of my server.lua. It is in fact printing the statement. For the tables that seemed empty, it turned out I was trying to read them in a way lua does not let you read tables, making the returning values nil.

However, I still wonder how to go about using config files on the server side, when they are safe from being manipulated and when they’re not.