Convars can be very powerful for defining configuration variables on the server. A convar is basically a configuration variable that you can set and get from any resource. You don't have to use a resource to get or set these values, you can also use commands in the (server) console or through a RCON client.
There are a couple of different convar types, we'll cover all of them on this page.
The most common of them all is just the 'standard' convar type.
You can set such convar by executing the following command:
set convar_name convar_value
If you want to set the value to a string that contains spaces, you can surround the convar_value
argument with quotation marks, like this:
set convar_name "This is a convar value that contains spaces!"
To get the value, simply type the convar name into the console, the value will then be printed to the console and will look something like this:
cfx> convar_name
"convar_name" is "This is a convar value that contains spaces!"
default: ""
type: string
If the convar has not been setup yet, typing convar_name
into the console will return the following message:
No such command convar_name.
You can also set and get convars using CFX API Set natives.
To set a basic convar value, you can either run ExecuteCommand()
with the command from the section above as the argument, this will require that the resource has the command.set
ace (permission) allowed.
Alternatively you can use the CFX API Set natives to set and get the values of convars. To set a value, use the following function, this example uses Lua, but it will work the same way in JS or C# resources:
SetConvar("convar_name", "This is a convar value that contains spaces!")
To get the convar value after it has been set, use the following function:
local convarValue = GetConvar("convar_name", "Default convar value.")
As you might have spotted already, you will need to provide a default value. This is because if the convar has not been set yet, the default value that you provided will be returned by the GetConvar value.
If you are sure that a convar will only contain an integer value, you may also use GetConvarInt("convar_name", 0)
in a similar way of the GetConvar()
function mentioned above. The second parameter being the default return value here as well.
Note, standard convars can only be used in server-side scripts. They can not be obtained or set from a client script.
If you want your convars to be synced between server and client scripts, then you will need to make the convar server replicated. This means that the convar will be available on the client side to get the value, but you will only be able to change the convar value from the server side.
Similar to the standard convars, you can make server replicated convars by using the following command:
setr convar_name convar_value
Use the following function to set a server replicated convar in a server script:
SetConvarReplicated("convar_name", "convar value")
To get the server replicated convar either from a server script or client script, use the same function that was used in the previous example:
local convarValue = GetConvar("convar_name", "Default convar value.")
These convars will act like 'standard' convars, however their name and value will appear on the server connect details/info page, on the server list, and will be exposed in the http://<server_ip>:<server_port>/info.json
server info file.
To set a server information convar, use the following command.
sets convar_name "Convar value."
A common example for this would be to have a server information convar named "Discord", and it's value set to the discord server related to your FiveM server.
sets Discord "Your discord link here"
This will look something like this on the server info page:
SetConvarServerInfo("convar_name", "Convar Value")
Here's a list of all CFX API set functions that you can use to get or set convars.