GetIronsights - error

Hello.

I have been getting this in the webconsole for days and I could not figure out the issue, it might be a simple one yet I havent gotten the key to it so far.

Here is the error:


[ERROR] gamemodes/terrortown/gamemode/util.lua:107: attempt to index field 'dt' (a number value)
1. GetIronsights - gamemodes/terrortown/gamemode/util.lua:107
2. unknown - gamemodes/terrortown/gamemode/player.lua:1066

util.lua - line 96 to 126


-- Color unpacking
function clr(color) return color.r, color.g, color.b, color.a; end

if CLIENT then
   -- Is screenpos on screen?
   function IsOffScreen(scrpos)
      return scrpos.x < 0 or scrpos.y < 0 or scrpos.x > ScrW() or scrpos.y > ScrH()
   end
end

function AccessorFuncDT(tbl, varname, name)
   tbl["Get" .. name] = function(s) return s.dt and s.dt[varname] end    -----------------------------------------------------   // This is line 107
   tbl["Set" .. name] = function(s, v) if s.dt then s.dt[varname] = v end end
end

function util.PaintDown(start, effname, ignore)
   local btr = util.TraceLine({start=start, endpos=(start + Vector(0,0,-256)), filter=ignore, mask=MASK_SOLID})

   util.Decal(effname, btr.HitPos+btr.HitNormal, btr.HitPos-btr.HitNormal)
end

local function DoBleed(ent)
   if not IsValid(ent) or (ent:IsPlayer() and (not ent:Alive() or not ent:IsTerror())) then
      return
   end

   local jitter = VectorRand() * 30
   jitter.z = 20

   util.PaintDown(ent:GetPos() + jitter, "Blood", ent)
end

player.lua - line 1025 to 1092


-- Drowning and such
local tm = nil
local ply = nil
local plys = nil
function GM:Tick()
   -- three cheers for micro-optimizations
   plys = player.GetAll()
   for i= 1, #plys do
      ply = plys*
      tm = ply:Team()
      if tm == TEAM_TERROR and ply:Alive() then
         -- Drowning
         if ply:WaterLevel() == 3 then
            if ply:IsOnFire() then
               ply:Extinguish()
            end

            if ply.drowning then
               if ply.drowning < CurTime() then
                  local dmginfo = DamageInfo()
                  dmginfo:SetDamage(15)
                  dmginfo:SetDamageType(DMG_DROWN)
                  dmginfo:SetAttacker(game.GetWorld())
                  dmginfo:SetInflictor(game.GetWorld())
                  dmginfo:SetDamageForce(Vector(0,0,1))

                  ply:TakeDamageInfo(dmginfo)

                  -- have started drowning properly
                  ply.drowning = CurTime() + 1
               end
            else
               -- will start drowning soon
               ply.drowning = CurTime() + 8
            end
         else
            ply.drowning = nil
         end

         -- Slow down ironsighters
         local wep = ply:GetActiveWeapon()
         if IsValid(wep) and wep:GetIronsights() then    -----------------------------------------------------   // This is line 1066
            ply:SetSpeed(true)
         else
            ply:SetSpeed(false)
         end

         -- Run DNA Scanner think also when it is not deployed
         if IsValid(ply.scanner_weapon) and wep != ply.scanner_weapon then
            ply.scanner_weapon:Think()
         end
      elseif tm == TEAM_SPEC then
         if ply.propspec then
            PROPSPEC.Recharge(ply)

            if IsValid(ply:GetObserverTarget()) then
               ply:SetPos(ply:GetObserverTarget():GetPos())
            end
         end

         -- if spectators are alive, ie. they picked spectator mode, then
         -- DeathThink doesn't run, so we have to SpecThink here
         if ply:Alive() then
            self:SpectatorThink(ply)
         end
      end
   end
end

I hope you can help me, I would really appreciate any comment from your side, even the slightest idea can help.

Thank you all for taking your time to read this :slight_smile:

I’ve had this problem before.
Make sure the base SWEP is being included correctly and make sure weaponry.lua is working without error.

Check for errors at initialize.

Ill check what errors I get on initialization.

I thought Id share this with you:

Well recently I have talked to Tommynator and he helped me and fixed the problem :slight_smile:

The problem was the spiderman gun.

Basically you have to change this:

self.dt = -1

into this:

self._dt = -1

I am going to provide the full code for the spiderman gun due to another error in it so you have a full fix.

I have been checking logs for the last 5 days, 0 errors from it :slight_smile:

I can finally read the console without getting spammed haha :smiley:

Here is the code:

Spiderman gun - shared.lua



if SERVER then
	AddCSLuaFile( "shared.lua" )
	resource.AddFile("materials/vgui/ttt/weapon_spiderman.vmt")
	resource.AddFile("materials/vgui/ttt/weapon_spiderman.vtf")
	resource.AddFile("sound/rope_hit.wav")
	resource.AddFile("sound/shoot_rope.wav")
	resource.AddFile("sound/to_far.wav")
end

	SWEP.HoldType			= "pistol"

if CLIENT then
	SWEP.PrintName			= "Spiderman's Gun"		
	SWEP.Slot				= 6
	SWEP.Icon = "vgui/ttt/weapon_spiderman" 
end

SWEP.CanBuy = { ROLE_TRAITOR, ROLE_DETECTIVE }
SWEP.LimitedStock = true
SWEP.AllowDrop = true
SWEP.AutoSpawnable = false

SWEP.EquipMenuData = {
   type = "Spidermans Gun",
   desc = "Zip through the skys as Spiderman would!"
};

SWEP.AmmoEnt			= "item_ammo_pistol_ttt"
SWEP.Kind				= WEAPON_EQUIP1
SWEP.Base				= "weapon_tttbase"
SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true

SWEP.DrawAmmo			= false
SWEP.DrawCrosshair		= true
SWEP.ViewModel			= "models/weapons/v_pistol.mdl"
SWEP.WorldModel			= "models/weapons/w_pistol.mdl"

local sndPowerUp		= Sound ("rope_hit.wav")
local sndPowerDown		= Sound ("shoot_rope.wav")
local sndTooFar			= Sound ("to_far.wav")

SWEP.IronSightsPos		= Vector( 6.05, -5, 2.4 )
SWEP.IronSightsAng		= Vector( 2.2, -0.1, 0 )

function SWEP:Initialize()

	nextshottime = CurTime()
	
end

function SWEP:Think()

	if (!self.Owner || self.Owner == NULL) then return end
	
	if ( self.Owner:KeyPressed( IN_ATTACK ) ) then
	
		self:StartAttack()
		
	elseif ( self.Owner:KeyDown( IN_ATTACK ) && inRange ) then
	
		self:UpdateAttack()
		
	elseif ( self.Owner:KeyReleased( IN_ATTACK ) && inRange ) then
	
		self:EndAttack( true )
	
	end
	
	if ( self.Owner:KeyPressed( IN_ATTACK2 ) ) then
	
		self:Attack2()
		
	end

end

function SWEP:DoTrace( endpos )
	local trace = {}
		trace.start = self.Owner:GetShootPos()
		trace.endpos = trace.start + (self.Owner:GetAimVector() * 14096)
		if(endpos) then trace.endpos = (endpos - self.Tr.HitNormal * 7) end
		trace.filter = { self.Owner, self.Weapon }
		
	self.Tr = nil
	self.Tr = util.TraceLine( trace )
end

function SWEP:StartAttack()
	local gunPos = self.Owner:GetShootPos()
	local disTrace = self.Owner:GetEyeTrace()
	local hitPos = disTrace.HitPos
	
	local x = (gunPos.x - hitPos.x)^2;
	local y = (gunPos.y - hitPos.y)^2;
	local z = (gunPos.z - hitPos.z)^2;
	local distance = math.sqrt(x + y + z);
	
	local distanceCvar = GetConVarNumber("rope_distance")
	inRange = false
	if distance <= distanceCvar then
		inRange = true						//100
	end
	
	if inRange then
		if (SERVER) then
			
			if (!self.Beam) then
				self.Beam = ents.Create( "rope" )
					self.Beam:SetPos( self.Owner:GetShootPos() )
				self.Beam:Spawn()
			end
			
			self.Beam:SetParent( self.Owner )
			self.Beam:SetOwner( self.Owner )
		
		end
		
		self:DoTrace()
		self.speed = 10000
		self.startTime = CurTime()
		self.endTime = CurTime() + self.speed
		self._dt = -1
		
		if (SERVER && self.Beam) then
			self.Beam:GetTable():SetEndPos( self.Tr.HitPos )
		end
		
		self:UpdateAttack()
		
		self.Weapon:EmitSound( sndPowerDown )
	else
		self.Weapon:EmitSound( sndTooFar )
	end
end

function SWEP:UpdateAttack()

	self.Owner:LagCompensation( true )
	
	if (!endpos) then endpos = self.Tr.HitPos end
	
	if (SERVER && self.Beam) then
		self.Beam:GetTable():SetEndPos( endpos )
	end

	lastpos = endpos
	
	
			if ( self.Tr.Entity:IsValid() ) then
			
					endpos = self.Tr.Entity:GetPos()
					if ( SERVER ) then
					self.Beam:GetTable():SetEndPos( endpos )
					end
			
			end
			
			local vVel = (endpos - self.Owner:GetPos())
			local Distance = endpos:Distance(self.Owner:GetPos())
			
			local et = (self.startTime + (Distance/self.speed))
			if(self._dt != 0) then
				self._dt = (et - CurTime()) / (et - self.startTime)
			end
			if(self._dt < 0) then
				self.Weapon:EmitSound( sndPowerUp )
				self._dt = 0
			end
			
			if(self._dt == 0) then
			zVel = self.Owner:GetVelocity().z
			vVel = vVel:GetNormalized()*(math.Clamp(Distance,0,7))
			end
				if( SERVER ) then
				local gravity = GetConVarNumber("sv_Gravity")
				vVel:Add(Vector(0,0,(gravity/100)*1.5))
				if tonumber(zVel) and zVel < 0 then
					vVel:Sub(Vector(0,0,zVel/100))
			end							//prob
				self.Owner:SetVelocity(vVel)
			end
	
	endpos = nil
	
	self.Owner:LagCompensation( false )
	
end

function SWEP:EndAttack( shutdownsound )
	
	if ( shutdownsound ) then
		self.Weapon:EmitSound( sndPowerDown )
	end
	
	if ( CLIENT ) then return end
	if ( !self.Beam ) then return end
	
	self.Beam:Remove()
	self.Beam = nil
	
end

function SWEP:Attack2()
			

	if (CLIENT) then return end
		local CF = self.Owner:GetFOV()
		if CF == 90 then
			self.Owner:SetFOV(30,.3)
		elseif CF == 30 then
			self.Owner:SetFOV(90,.3)
	end
end

function SWEP:Holster()
	self:EndAttack( false )
	return true
end

function SWEP:OnRemove()
	self:EndAttack( false )
	return true
end


function SWEP:PrimaryAttack()
end

function SWEP:SecondaryAttack()
end


I am sorry for a bump but I think if people are experiencing the problem aswell it would be nice to have a fix out there.