Annoying Unfixable Error

This error keeps popping up everytime I shoot my entity.

includes/extensions/table.lua:165: bad argument #1 to ‘pairs’ (table expected, got nil)

Paste your entity’s code here so we have a clue to what you are doing that might be raising the error.

Well it consists of an entity base, and entity.
And I used some of Rambo_6’s code as well, but.

Base Init file:

AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

Vo = {}

Vo.Claw = {“npc/zombie/claw_strike1.wav”,
“npc/zombie/claw_strike2.wav”,
“npc/zombie/claw_strike3.wav”}

for k,v in pairs(Vo) do
for c,d in pairs(Vo[k]) do
util.PrecacheSound(d)
end
end

//function ChooseRandom(tablename)
//return tablename[math.random(1,table.Count(tablename))] end

function ENT:SpawnFunction( ply, tr )

if ( !tr.Hit ) then return end

local SpawnPos = tr.HitPos + tr.HitNormal * 20

local ent = ents.Create( "npc_base_g" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()

return ent

end

//local Ghosts = //{
//“npc_ghost”,
//“npc_ghost_hidden”,
//“npc_base_g”
//}

//hook.Add(“CanTool”,“GhostSNPCV2_NoTool”,function(ply,tr,mode)
//if !tr.Entity or !tr.Entity:IsValid() or table.HasValue(Ghosts,tr.Entity:GetClass()) then
//return false;
//else
//return true
//end

//end)

function ENT:Initialize()

self:SetModel( "models/zombie/classic.mdl" )
util.PrecacheSound("npc/zombie/zo_attack2.wav")

self:SetHullType( HULL_HUMAN );
self:SetHullSizeNormal();

self:SetSolid( SOLID_BBOX ) 
self:SetMoveType( MOVETYPE_STEP )

self:CapabilitiesAdd( CAP_MOVE_GROUND | CAP_ANIMATEDFACE | CAP_TURN_HEAD | CAP_USE_SHOT_REGULATOR | CAP_AIM_GUN )

self:SetMaxYawSpeed( 5000 )

self:SetHealth(math.random(90,110))

end

function ENT:VoiceSound(sound,vol)
vol = vol or 100
local rnum = math.random(90,100)
if self.SoundDelay then
if self.SoundDelay < CurTime() then
self.SoundDelay = CurTime() + 1.5
self:EmitSound(sound,vol,rnum)
WorldSound( sound, self:GetPos(), 75, rnum )
end
else
self.SoundDelay = CurTime() + 1.5
WorldSound( sound, self:GetPos(), 75, rnum )
end
end

function ENT:DoDeath(dmginfo)

local ghost = (self.entity)

if self.IsDead then self:Remove() return end
self.IsDead = true

local ghost = dmginfo:GetAttacker()

self:VoiceSound(ChooseRandom(self.Death),100)
		
self:SetSchedule( SCHED_FALL_TO_GROUND )
self:Remove()

end

function ENT:OnTakeDamage(dmg)

if not dmg:GetAttacker():IsPlayer() then
	dmg:SetDamage(0)
end
self:SetHealth(math.Clamp(self:Health() - dmg:GetDamage(),0,100))
if self:Health() &lt;= 0 then
	if self:IsValid() then
		self:DoDeath(dmg)
	end
else
	if math.random(1,1) == 1 then
		self:VoiceSound(ChooseRandom(self.Pain),100)
	end
end

end

function ENT:Think()
if not self.IdleTalk then
self.IdleTalk = CurTime() + math.random(1,5)
elseif self.IdleTalk < CurTime() then
self:VoiceSound(ChooseRandom(self.Taunt),100)
self.IdleTalk = nil
end
if not self.Locator then
self.Locator = CurTime() + 2
elseif self.Locator < CurTime() then
self.Locator = nil
if self.CurEnemy and self.CurEnemy != NULL and self.CurEnemy:IsValid() then
if self.CurEnemy:Alive() then
self:SetEnemy( self.CurEnemy, true )
self:UpdateEnemyMemory( self.CurEnemy, self.CurEnemy:GetPos() )
else
if table.Count(player.GetAll()) < 1 then
self:SetEnemy( NULL )
self.CurEnemy = nil
else
local enemy = ChooseRandom(player.GetAll())
self:SetEnemy( enemy, true )
self:UpdateEnemyMemory( enemy, enemy:GetPos() )
self.CurEnemy = enemy
end
end
end
end
end

ENT.OldPos = Vector(0,0,0)
function ENT:TaskStart_FindEnemy( data )
if self.OldPos:Distance(self:GetPos()) < 5 then
if table.Count(player.GetAll()) >= 1 then
self.CurEnemy = ChooseRandom(player.GetAll())
else
self.CurEnemy = NULL
self:SetEnemy( self.CurEnemy, true )
self:TaskComplete()
return
end
self.OldPos = self:GetPos()
self:SetEnemy( self.CurEnemy, true )
self:UpdateEnemyMemory( self.CurEnemy, self.CurEnemy:GetPos() )
self:TaskComplete()
return
end
local enemies = player.GetAll()
if table.Count(enemies) >= 1 then
if self.CurEnemy then
if self.CurEnemy:Alive() then
local dist = self.CurEnemy:GetPos():Distance(self:GetPos())
local enemy = self.CurEnemy
for k,v in pairs(enemies) do
if v:GetPos():Distance(self:GetPos()) < dist then
dist = v:GetPos():Distance(self:GetPos())
enemy = v
end
end
self.OldPos = self:GetPos()
self.CurEnemy = enemy
self:SetEnemy( enemy, true )
self:UpdateEnemyMemory( enemy, enemy:GetPos() )
self:TaskComplete()
else
local enemy = ChooseRandom(enemies)
self:SetEnemy( enemy, true )
self:UpdateEnemyMemory( enemy, enemy:GetPos() )
self.CurEnemy = enemy
self:TaskComplete()
end
else
local enemy = NULL
local dist = 999999
for k,v in pairs(enemies) do
if v:GetPos():Distance(self:GetPos()) < dist then
dist = v:GetPos():Distance(self:GetPos())
enemy = v
end
end
self.OldPos = self:GetPos()
self.CurEnemy = enemy
self:SetEnemy( enemy, true )
self:UpdateEnemyMemory( enemy, enemy:GetPos() )
self:TaskComplete()
end
else
self:SetEnemy( NULL )
end
end

function ENT:TaskStart_PlaySequence( data )

local SequenceID = data.ID

if ( data.Name ) then SequenceID = self:LookupSequence( data.Name )	end

self:ResetSequence( SequenceID )
self:SetNPCState( NPC_STATE_SCRIPT )

local Duration = self:SequenceDuration()

if ( data.Speed && data.Speed &gt; 0 ) then 

	SequenceID = self:SetPlaybackRate( data.Speed )
	Duration = Duration / data.Speed

end

self.TaskSequenceEnd = CurTime() + Duration
self.HitTime = CurTime() + (Duration / self.AnimScale)
self:EmitSound(ChooseRandom(self.Attack),100,math.random(85,100))

end

function ENT:Task_PlaySequence( data )

if ( CurTime() &gt; self.HitTime ) and not self.DidHit then
	self.DidHit = true
	
	for k,v in pairs(player.GetAll()) do
		if v:GetPos():Distance(self:GetPos()) &lt; 50 then
			if v:KeyDown(IN_DUCK) then
				v:TakeDamage(self.Damage,self)
				v.DeathType = "normal"
				self:EmitSound(ChooseRandom(Vo.Claw),100,math.random(90,110))
			end
		end
	end
	
	local trace = {}
	trace.start = (self:GetPos() + Vector(0,0,60)) + (self:GetAngles():Forward() * -10)
	trace.endpos = trace.start + self:GetAngles():Forward() * 110
	trace.filter = self
	local tr = util.TraceLine(trace) 
	if tr.HitWorld or not tr.Entity:IsValid() then 
		return
	end
	if tr.Entity:IsPlayer() then
		self:EmitSound(ChooseRandom(Vo.Claw),100,math.random(90,110))
		tr.Entity:TakeDamage(self.Damage,self,self)
		if math.random(1,3) == 1 then
			self:VoiceSound(ChooseRandom(self.Taunt),100)
		end
		self:EmitSound(ChooseRandom(Vo.Claw),100,math.random(90,110))
	elseif string.find(tr.Entity:GetClass(),"prop_phys") then
		self:EmitSound(ChooseRandom(Vo.Claw),100,math.random(90,110))
		tr.Entity:TakeDamage(self.Damage,self,self)
		local phys = tr.Entity:GetPhysicsObject()
		if phys:IsValid() then
			phys:ApplyForceCenter(self:GetForward() * 2000)
		end
	end
end

if CurTime() &lt; self.TaskSequenceEnd then return end

self:TaskComplete()
self.DidHit = false
self:SetNPCState( NPC_STATE_NONE )

// Clean up
self.TaskSequenceEnd = nil

end

[editline]07:53PM[/editline]

Entity Init File:

AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

ENT.SpawnRagdollOnDeath = false
ENT.AnimScale = 3
ENT.Damage = 13
ENT.Death = {“vo/npc/male01/no01.wav”,“vo/npc/male01/no02.wav”,“vo/npc/male01/goodgod.wav”,“vo/npc/male01/gordead_ans02.wav”,“vo/npc/female01/no01.wav”,“vo/npc/female01/no02.wav”,“vo/npc/female01/notthemanithought01.wav”,“vo/npc/female01/notthemanithought02.wav”}
ENT.Taunt = {“vo/npc/male01/wetrustedyou01.wav”,“vo/npc/male01/wetrustedyou02.wav”,“vo/npc/male01/heretohelp01.wav”,“vo/npc/male01/heretohelp02.wav”,“vo/npc/male01/evenodds.wav”,“vo/npc/male01/notthemanithought01.wav”,“npc/nihilinth/nil_freeman.wav”,“vo/npc/female01/abouttime01.wav”,“vo/npc/female01/abouttime02.wav”,“vo/npc/female01/ahgordon01.wav”,“vo/npc/female01/ahgordon02.wav”,“vo/npc/female01/answer40.wav”,“vo/npc/female01/fantastic02.wav”,“vo/npc/female01/finally.wav”,“vo/npc/female01/gethellout.wav”,“vo/npc/female01/abouttime01.wav”,“vo/npc/female01/abouttime02.wav”,“vo/npc/female01/ahgordon01.wav”,“vo/npc/female01/ahgordon02.wav”,“vo/npc/female01/answer40.wav”,“vo/npc/female01/fantastic02.wav”,“vo/npc/female01/finally.wav”,“vo/npc/female01/gethellout.wav”,“vo/npc/female01/moan01.wav”,“vo/npc/female01/moan02.wav”,“vo/npc/female01/moan03.wav”,“vo/npc/female01/moan04.wav”,“vo/npc/female01/moan05.wav”,“vo/npc/female01/question18.wav”,“vo/npc/female01/question03.wav”,“vo/npc/female01/runforyourlife01.wav”,“vo/npc/female01/runforyourlife02.wav”,“vo/npc/female01/stopitfm.wav”,“npc/nihilinth/laughter.wav”,“npc/nihilinth/spook1.wav”,“npc/nihilinth/spook2.wav”,“vo/npc/male01/gordead_ques17.wav”,“vo/npc/male01/moan01.wav”,“vo/npc/male01/moan02.wav”,“vo/npc/male01/moan03.wav”,“vo/npc/male01/moan04.wav”,“vo/npc/male01/moan05.wav”,“vo/npc/male01/question18.wav”,“vo/npc/male01/question03.wav”,“vo/npc/male01/runforyourlife01.wav”,“vo/npc/male01/runforyourlife02.wav”,“vo/npc/male01/thislldonicely.wav”}
ENT.Attack = {“vo/npc/male01/abouttime02.wav”,“vo/npc/male01/excuseme01.wav”,“vo/npc/male01/fantastic01.wav”,“vo/npc/male01/finally.wav”,“vo/npc/male01/likethat.wav”,“vo/npc/male01/stopitfm.wav”,“vo/npc/male01/yeah02.wav”,“vo/npc/female01/abouttime01.wav”,“vo/npc/female01/abouttime02.wav”,“vo/npc/female01/ahgordon01.wav”,“vo/npc/female01/ahgordon02.wav”,“vo/npc/female01/answer40.wav”,“vo/npc/female01/fantastic02.wav”,“vo/npc/female01/finally.wav”,“vo/npc/female01/gethellout.wav”,“vo/npc/female01/gordead_ans09.wav”,“vo/npc/female01/gordead_ques01.wav”,“vo/npc/female01/gordead_ques13.wav”,“vo/npc/female01/gordead_ques17.wav”,“vo/npc/female01/hellodrfm01.wav”,“vo/npc/female01/hellodrfm02.wav”,“vo/npc/female01/pardonme02.wav”,“vo/npc/female01/thislldonicely01.wav”,“vo/npc/female01/wetrustedyou01.wav”,“vo/npc/female01/wetrustedyou02.wav”,“vo/npc/female01/whoops01.wav”,“vo/npc/female01/yeah02.wav”,“vo/npc/male01/ahgordon01.wav”,“vo/npc/male01/ahgordon02.wav”,“vo/npc/male01/answer39.wav”,“vo/npc/male01/answer01.wav”,“vo/npc/male01/gethellout.wav”,“vo/npc/male01/gordead_ans09.wav”,“vo/npc/male01/gordead_ques01.wav”,“vo/npc/male01/question13.wav”}
ENT.MeleeAnims = {“swing”,“throw1”,“throwitem”}

function ENT:SpawnFunction( ply, tr )

if ( !tr.Hit ) then return end

local SpawnPos = tr.HitPos + tr.HitNormal * 20

local ent = ents.Create( "npc_test" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()

return ent

end

function ENT:Initialize()

self:SetModel( "models/humans/group01/male_04.mdl" )

self:SetHullType( HULL_HUMAN );
self:SetHullSizeNormal();

self:SetSolid( SOLID_BBOX ) 
self:SetMoveType( MOVETYPE_STEP )

self:CapabilitiesAdd( CAP_MOVE_GROUND | CAP_ANIMATEDFACE | CAP_TURN_HEAD | CAP_AIM_GUN | CAP_OPEN_DOORS | CAP_MOVE_JUMP | CAP_MOVE_CLIMB )

self:SetMaxYawSpeed( 5000 )

self:SetHealth ( 501 )

    self:SetColor(255, 255, 255, 0)

end

function ENT:Think()
if not self.IdleTalk then
self.IdleTalk = CurTime() + math.random(3,9)
elseif self.IdleTalk < CurTime() then
self:VoiceSound(ChooseRandom(self.Taunt),100)
self.IdleTalk = True
end

self.Entity:SetVelocity(self.Entity:GetForward()*120)

end

function ENT:SelectSchedule()

local schdChase = ai_schedule.New( "Test AI" ) //creates the schedule used for this npc

schdChase:AddTask( "FindEnemy", 		{ Class = "player", Radius = 10000000 } )
schdChase:EngTask( "TASK_GET_PATH_TO_ENEMY", 0 )
schdChase:EngTask( "TASK_RUN_PATH_TIMED", 0.2 )
schdChase:EngTask( "TASK_WAIT", 0.2 ) 

local close = false
local tbl = player.GetAll()
for k,v in pairs(tbl) do
	if v:GetPos():Distance(self:GetPos()) &lt; 75 then
		close = true
	end
end

if close then

self.Entity:SetVelocity(self.Entity:GetForward()*0)

	schdChase:EngTask( "TASK_STOP_MOVING", 			0 )
	schdChase:EngTask( "TASK_FACE_ENEMY", 			0 )
	schdChase:AddTask( "PlaySequence", 				{ Name = ChooseRandom(self.MeleeAnims), Speed = 1.9 } )
end

self:StartSchedule( schdChase ) 

end

What your error says is that somewhere in your code you’re giving the pairs function (for k,v in pairs(something)) a bad argument. That must mean this argument is not defined/does not exist.

Also when posting your code please place it inside [lua] tags so the indentation and readability remain.

Like, as in removed? I think that might be the case seeing as the base code is kind of old…however I don’t know where I would find the bad argument.

[editline]08:03PM[/editline]

Also, this strangely does not happen to my other entity that uses this base code as well…

Or he’s using a function that expects a table and wasn’t given one.

[editline]02:06AM[/editline]

That ChooseRandom function expects a table.

You have:

ChooseRandom( self.Pain )

self.Pain is nil. You need to define it somewhere in your code.

Thank you Kogitsune, It’s working fine now :slight_smile: sorry for the bother.