Garry's Mod Wiki

CreateConVar

  ConVar CreateConVar( string name, string value, number flags = FCVAR_NONE, string helptext = "", number min = nil, number max = nil )

Description

Creates a console variable (ConVar), in general these are for things like gamemode/server settings.

Do not use the FCVAR_NEVER_AS_STRING and FCVAR_REPLICATED flags together, as this can cause the console variable to have strange values on the client.

Arguments

1 string name
Name of the ConVar.

This cannot be a name of an engine console command or console variable. It will throw an error if it is. If it is the same name as another lua ConVar, it will return that ConVar object.

2 string value
Default value of the convar. Can also be a number.
3 number or table flags = FCVAR_NONE
Flags of the convar, see FCVAR enum, either as bitflag or as table.
4 string helptext = ""
The help text to show in the console.
5 number min = nil
If set, the ConVar cannot be changed to a number lower than this value.
6 number max = nil
If set, the ConVar cannot be changed to a number higher than this value.

Returns

1 ConVar
The convar created.

Example

Code that will create a Convar and a chatprint with the content of the convar.

local convar_example = CreateConVar("convar_example", "1", {FCVAR_ARCHIVE, FCVAR_NOTIFY, FCVAR_REPLICATED}, "This is a server-side convar.") concommand.Add("print_convar", function(ply) ply:ChatPrint("Server ConVar value: " .. convar_example:GetString()) end)
Output: Server ConVar value: 1