Explain to me how configs work?

Hi, I am making a script and I am needing to make a config. I made a file named “sh_config.lua” located in “addonname/lua/sh_config.lua” and added it to cs lua. I am wanting to make it where the user can type in jobs like “Citizen” and such and then target the jobs they typed in the config in my code. How would I do that?

Most likely you’d use a global table or return the table in the file to be used with

include.

Yeah I understand that but what is the specific way of doing it. Is this it?

CONFIG




JobsToTarget = {

"Citizen",
"Civil Protect",

}



SERVER




for k,v in pairs(JobsToTarget)

v:Blah BLah Blah, whatever to the job.

end



A config file is a file that makes it easy to edit certain aspects of whatever you’re making.
Say you are making a vehicle or something and you wanted other people to edit certain aspects of it. Your config might look like this -



vehicle = {}
vehicle.speed = 100
vehicle.weight = 200


There are better ways of implementing settings like that but I think you get the idea. You should never have to loop through your config file settings, unless it was a pretty specific situation. For example, if I was making a gamemode and I wanted to give users the ability to add certain things, like player models, then I would do something similar to what you did above.
Something maybe like this:



gamemode = {}
gamemode.rounds = 5 // this is just for show
gamemode.playermodels = {
"player/model1/path.mdl",
"player/model2/path.mdl",
"player/model3/path.mdl"
}


Ok so the config what I said would work, what about accessing every single thing in the table for use in server.lua

Accessing the entire table at once server side is just a loop. The way you did above works just fine.

Yeah so I did it and it does work but I came across another thing I need to fix. How do I access specific ones? Heres what I mean :

CONFIG



color = {
    r = 255
    g = 0
    b = 0
}


SERVER



local color = Color(r, g, b)


Get what I mean? I want to access specific ones.

Accessing a variable from within a table can be done like this -



local color = Color( color.r, color.g, color.b )


Edit:
If your table doesn’t have a defined index, or you structured the table this way:



table = {
"Mary",
"Had",
"Lamb"
}

// To access the table:
var = table[1]
// or
var = table[2]


All of this is information you can find if you search for it though.
This should help you - https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index2584.html
It’s the old wiki, but still has a lot of useful information in it.

oh okay thankyou!!!

[editline]10th August 2017[/editline]

Well now I got a error :frowning:




ConfigM = {
	OpeningText = "Blah " -- Set to "" for nothing.  =1=
	JobText = "M" -- The text of the job in chat.  =2=
	UseUserName = true -- Use the user name?  =3=
	OpenColor = Color(182, 0, 0) -- The opening Color that is used in  1, 2, 3
	CloseColor = Color(255, 255, 255) -- The closing Color that is used in  4
}



ERROR :




[ERROR] addons/namer/lua/sh_config.lua:62: '}' expected (to close '{' at line 60) near 'JobText'
  1. unknown - addons/namer/lua/sh_config.lua:0




[editline]10th August 2017[/editline]

This is whats using JobText and stuff (CLIENT) : – EDIT : I mean client lol




	if callerType == 1 then
		for k, v in pairs(JobsForMedic) do
			if LocalPlayer():getDarkRPVar("job") == v then
				if ConMedic.UseUserName == true then
					chat.AddText(ConMedic.OpenColor, ConMedic.OpeningText .. ConMedic.JobText .. LocalPlayer():Nick(), ConMedic.CloseColor, callerMessage)
				else
					chat.AddText(ConMedic.OpenColor, ConMedic.OpeningText .. ConMedic.JobText, ConMedic.CloseColor, callerMessage)
				end
			end
		end
	end



[editline]10th August 2017[/editline]

Its because I didnt have the commas :frowning:

You need a comma at the end of each line, before your comments, in your config

EDIT: Didn’t see you already solved it, my bad