Concommand Access?

Okay, what I’m trying to do right now is make a check in my function for if a player is aiming down their weapon’s sight. Here is the error I’m getting when I use the FLIR command in console:


[ERROR] gamemodes/ccs/gamemode/client/cl_infared.lua:72: attempt to index global 'wep' (a nil value)
  1. unknown - gamemodes/ccs/gamemode/client/cl_infared.lua:72
   2. unknown - lua/includes/modules/concommand.lua:69

Here is the code for my “client/cl_infared.lua” file where this is all done:


if not CLIENT then return end

local sndOn = Sound( "items/nvg_on.wav" )
local sndOff = Sound( "items/nvg_off.wav" )

local DoXRay = false

local DefMats = {}
local DefClrs = {}

local pairs = pairs
local string = string
local render = render

local ColorTab = 
{
	[ "$pp_colour_addr" ] 		= -.4,
	[ "$pp_colour_addg" ] 		= -.5,
	[ "$pp_colour_addb" ] 		= -.5,
	[ "$pp_colour_brightness" ] 	= .1,
	[ "$pp_colour_contrast" ] 	= 1,
	[ "$pp_colour_colour" ] 	= 0,
	[ "$pp_colour_mulr" ] 		= 0,
	[ "$pp_colour_mulg" ] 		= 0,
	[ "$pp_colour_mulb" ] 		= 0,
}

local function XRayMat()

	for k,v in pairs( ents.GetAll() ) do
	
		if string.sub( (v:GetModel() or "" ), -3) == "mdl" then
		
			local r,g,b,a = v:GetColor()
			if 1 == 1 then
				
				local entmat = v:GetMaterial()

				if v:IsNPC() or v:IsPlayer() then
					
					if entmat ~= "infared/living" then
						DefMats[ v ] = entmat
						v:SetMaterial( "infared/living" )
					end
				end
			end
		end
	end
end


-- This is where we do the post-processing effects.
local function XRayFX() 

	-- Colormod
	DrawColorModify( ColorTab ) 
	
	-- Bloom
	DrawBloom(	0,  					-- Darken
 				1,				-- Multiply
 				1, 				-- Horizontal Blur
 				1, 				-- Vertical Blur
 				0, 				-- Passes
 				0, 				-- Color Multiplier
 				0, 				-- Red
 				0, 				-- Green
 				0 ) 			-- Blue
	
end 


function FLIROff()

	if DoXRay then
	
		hook.Remove( "RenderScene", "XRay_ApplyMats" )
		hook.Remove( "RenderScreenspaceEffects", "XRay_RenderModify" )

		DoXRay = false
		surface.PlaySound( sndOff )
		
		-- Set colors and materials back to normal
		for ent,mat in pairs( DefMats ) do
			if ent:IsValid() then
				ent:SetMaterial( mat )
			end
		end
		
		for ent,clr in pairs( DefClrs ) do
			if ent:IsValid() then
				ent:SetColor( clr.r, clr.g, clr.b, clr.a )
			end
		end
		
		DefMats = {}
		DefClrs = {}
	end
end

function FLIR()
	if DoXRay and IsValid(wep) and wep:GetIronsights() then
			hook.Add( "RenderScene", "XRay_ApplyMats", XRayMat )
			hook.Add( "RenderScreenspaceEffects", "XRay_RenderModify", XRayFX )

			DoXRay = true
			surface.PlaySound( sndOn )

	else
		hook.Remove( "RenderScene", "XRay_ApplyMats" )
		hook.Remove( "RenderScreenspaceEffects", "XRay_RenderModify" )

		DoXRay = false
		surface.PlaySound( sndOff )
		
		for ent,mat in pairs( DefMats ) do
			if ent:IsValid() then
				ent:SetMaterial( mat )
			end
		end
		
		for ent,clr in pairs( DefClrs ) do
			if ent:IsValid() then
				ent:SetColor( clr.r, clr.g, clr.b, clr.a )
			end
		end
		
		DefMats = {}
		DefClrs = {}
	end
end

concommand.Add( "FLIR", FLIR )

if ply:GetActiveWeapon():GetClass() != “weaponname” then return end ?

Accidental post, go back to top please.

Bump.

Bump.

Where are you defining wep?

You should, since it’s CLIENT, probably do local wep = LocalPlayer( ):GetActiveWeapon( ); at the top of that function and see if that fixes it.

So that way it’ll be nil (probably error out actually) and nothing will work?

Instead of this:
[lua]function FLIR()
if DoXRay and IsValid(wep) and wep:GetIronsights() then[/lua]

This:
[lua]function FLIR()
local wep = LocalPlayer( ):GetActiveWeapon( );

// Your error is on the next line because "wep" isn't defined anywhere in the scope of this function, or outside the scope of this function and BEFORE the function is called. Next, since this is CLIENT code, we can use LocalPlayer( ) and get their active weapon...
if DoXRay and IsValid(wep) and wep:GetIronsights() then[/lua]

Does that make more sense?

Following on what Acecool said, replace this

[lua]if DoXRay and IsValid(wep) and wep:GetIronsights() then[/lua]
with
[lua]if DoXRay and IsValid(wep) and wep.GetIronsights and wep:GetIronsights() then[/lua]

as default weapons don’t even have that function.

Hey everybody, I’m very sorry for not responding recently, I’ve just been caught up with tons of work, anyways, I’ll review all of your suggestions right now!

[editline]22nd September 2013[/editline]

Okay, thank you all for your help everyone! I’ve managed to get this to work thanks to your help!

I’ll be sure to post some screenshots soon!