Getting a client's convar number?

Hello,

How would I be able to get a client’s convar number, lets say, sv_cheats?

Would this work?


function checkNum(ply)
local clientValue = GetConVarNumber( sv_cheats )
if clientValue > 1 then
ply:Kick
else return end
end
end

You’re going to most likely use this function
ply:GetInfoNum( sv_cheats, clientvalue )
to set clientvalue to whatever that players value is.

I’m assuming you have this function hooked to PlayerInitialSpawn or something else so the player is actually checked.

Edit: Just Noticed you are Missing () on the Kick Function. It should be ply:Kick() not ply:Kick.

Yes, it’s hooked.



function checkNum(ply)
local clientValue = GetConVarNumber( sv_cheats ) -- clientValue == 1
scan = ply:GetInfoNum( sv_cheats, clientvalue ) -- Getting client value of sv_cheats
if scan > 1 then -- if it returns 1 then -- comparing values
ply:Kick() -- if weird then kick
else return end -- else do nothing
end
end


Edit: Yeah, I noticed I forgot the params on the kick.

Nar, you don’t need to use GetConVarNumber as your retrieving that number from ply:getInfoNum. Also you needed to make it greater than or equal to.
I fixed your code for you to show you what I mean:



function checkNum(ply)
	clientvalue = ply:GetInfoNum( sv_cheats, 0 ) -- Getting client value of sv_cheats

	if clientvalue => 1 then -- if it returns 1 then -- comparing values
		ply:Kick() -- if weird then kick
	else 
		return -- else do nothing
	end
end


Edit: I made a stupid error and was wrong about the second argument, thats used to specify what to assume the variable is if it does not return anything. My Bad.

If this doesn’t work just get back to me.





if SERVER then
	hook.Add( "PlayerInitialSpawn", "ClientConVarCheck", function(ply)
		clientvalue = ply:GetInfoNum( sv_cheats, 0 ) -- Getting client value of sv_cheats
		if(clientvalue => 1) then -- if it returns 1 then -- comparing values
			ply:Kick() -- if weird then kick
		else 
			return -- else do nothing
		end
	end)
end


I add that code to a lua file (a new one, really)

and it’s located inside addons.

However, it doesn’t work. Any help?

You put the lua file under the lua/autorun folder in your addon right?

ply:Kick() needs an argument … like ply:Kick(“No reason given.”)

Yeah, its under

garrysmod/addons/nDraw/lua/autorun/server

Make sure your addon as an addon.txt or the addon won’t run. To be sure the function even works, try putting it in the main gmod lua/autorun.

Also Nak, thank you I wasn’t aware you HAVE to have that argument specified.

Here’s the Function with the Change



if SERVER then
	hook.Add( "PlayerInitialSpawn", "ClientConVarCheck", function(ply)
		clientvalue = ply:GetInfoNum( "sv_cheats", 0 ) -- Getting client value of sv_cheats
		if(clientvalue >= 1) then -- if it returns 1 then -- comparing values
			ply:Kick("Forcing the sv_cheats variable to 1") -- if weird then kick
		else 
			return -- else do nothing
		end
	end)
end


edit: Changed => to >=, sorry for the silly mistake. Late at night and I am making nonstop silly Mistakes. Fixed quote error.

This is odd.

[ERROR] lua/autorun/cv.lua:18: ‘then’ expected near ‘=’

  1. unknown - lua/autorun/cv.lua:0

cv.lua is the script.

And its in lua/autorun this time.

Line 18 is: if clientvalue => 1 then – if it returns 1 then – comparing values

That’s because it’s >=, not =>

Danget, I made another stupid mistake? I always thought that didn’t matter. Thank you for the correction code_gs.

I apologize for that mistake.

[lua]ply:GetInfoNum( sv_cheats, 0 )[/lua]

How do you expect it to work without quote marks?

[lua]ply:GetInfoNum( “sv_cheats”, 0 )[/lua]

It really must be too late at night for me to be doing this. So much for me helping someone. Thanks.

I’m pretty sure all of the silly mistakes are gone now. I updated that post with the fixes so it should work now.