Troubles with CreateConVar and GetConVarNumber

Here’s my problem. I’m working on a gamemode and I have an option for how many players are required before the round starts (a la TTT). Currently, one has to change the value manually in a lua file, but I’d like to create a ConVar so one can easily set common values through server.cfg or otherwise. Here is my code:


-- in gamemode/init.lua
GM.minPlayers 	= CreateConVar( "sb_minplayers", 2, bit.bor(FCVAR_NOTIFY), "Nuber of players needed for the round to start" )  -- name, default value, Enums/FCVAR, help text

-- in sh_roundtimer.lua
round.MinPlayers 	= GetConVarNumber( "sb_minplayers" )

function enoughPlayers()
	local numPlayers = 0

	if table.Count(player.GetAll()) >= round.MinPlayers then
		if SERVER then		
			for k, v in pairs(player.GetAll()) do
				if IsValid(v) then
					v:PrintMessage(HUD_PRINTCENTER, "Round started! You have " .. math.Round(round.Time/60) .. " minutes!")
					v:SetTeam(TEAM_HUMANS)
					v:Spawn()
					v:StripWeapon("sb_barrelsuicider")
				end
			end
				
			local randomply = table.Random(player.GetAll())
				
			randomply:SetTeam(TEAM_BARRELS)
			randomply:Spawn()
			randomply:StripWeapon("sb_barrelslayer")
			randomply:PrintMessage(HUD_PRINTCENTER, "Round started! You are a Barrel! You have " .. math.Round(round.Time/60) .. " minutes to eliminate the humans!")
		end
		round.TimeLeft = round.Time
		round.Active = true
		round.RoundCur = round.RoundCur + 1
	elseif table.Count(player.GetAll()) < round.MinPlayers then
		if SERVER then
			for k, v in pairs(player.GetAll()) do
				if IsValid(v) then
					v:SetTeam(TEAM_SPECTATOR)
				end
			end
		end
		round.Active = false
	end
end

Despite all this, it appears that the value is not recognized. The round does not start, even if there are more than 2 people on the server.

You may have issues with creating convars for server.cfg

If I recall correctly, server.cfg is called before the convars have a chance to be made… I may be wrong, but here is a way to create convars:

https://dl.dropboxusercontent.com/u/26074909/tutoring/convars/creating_and_using_client_convars.lua.html

Could’ve sworn I had more tutorials on this, I’ll have to add more.

Try
[lua]
GetConVar(“sb_minplayers”):GetInt()
[/lua]

Instead of saving the value to a variable every time server starts, retrieve the value every time it is needed, so


if table.Count(player.GetAll()) >= round.MinPlayers then

becomes


if table.Count(player.GetAll()) >= GetConVarNumber( "sb_minplayers" ) then

or even


if #player.GetAll() >= GetConVarNumber( "sb_minplayers" ) then

Oh yes totally lets teach people how to unoptimize their code. You should only be calling GetConVar* if you’re going to be checking the number for changes every time you call the code…
Obviously in this situation you’re not going to be calling it that often, but if it was something in a HUD or Think hook, and he followed your advice it would slow down that function considerably.
If you had actually read the code you might have considered telling him to change the if statement to GM.minPlayers:GetInt(), because he’s saving a refrence to the ConVar and then looking up the convar again when he doesn’t need to be.

Oh yes, lets totally say nonsense.

This is exactly the case here. He needs to check for a new value every time to see if convar changed or not. This might even solve the server.cfg problem.

Obviously he isn’t making a HUD.

If you actually had read the code, you might have realized that GM.minPlayers equals to GetConVarNumber( “sb_minplayers” ) and returns a number, not a ConVar object.

CreateConVar returns a ConVar object though?

Darn, I confused GM.minPlayers and round.MinPlayers, my bad.

Nope

You’re essentially telling him what he is doing is wrong without telling him why thus in all likelyhood if he had followed your advice he might have done this in some other code.

I’m sorry, I don’t think I made my intention clear. I’m basically aping the idea behind the ttt_minimum_players setting from TTT. I’d like to be able to check the value of sb_minplayers and use that for round.minPlayers (i.e. if in server.cfg sb_minplayers = 3, the round will start when there are 3 players present, like TTT).

If I replace


round.MinPlayers = GetConVarNumber( "sb_minplayers" )

with


round.MinPlayers = 2

everything works fine. The round starts if there are 2 people. However, when round.MinPlayers = GetConVarNumber( “sb_minplayers” ) is set, the round doesn’t start. No errors show up in console, but, as I said, the round won’t start either.

From what Acetool and Blasteh have said, I’ll try chaning my code to the following once I get home:


round.minPlayers = GetConVar("sb_minplayers"):GetInt()

Also, Robotboy655, I’ll update my code to do what you said, for now I’m using a variable because that’s how the original code was, I’ll try your method as well.

EDIT:
Upon replacing all the instances of round.MinPlayers with GetConVarNumber( “sb_minplayers” ), I get the following error:

http://puu.sh/9pKzE/b2f240dd7b.png

I have a feeling that this conVar isn’t being created properly somehow, here’s my code for creating the conVars:


CreateConVar( "sb_minplayers", "2" )

However, when I plug the conVar into console, I get the following:

http://puu.sh/9pJXB/2224472cab.png

I’m not sure why no default value is being set, what would the reason for this be?