Attempt to call Method, nil value?

So i have a civilian bot, and i want it to be able to know when Civil Protection bots are nearby.
i named my civil protection bot “cp_nextbot”.


function ENT:IsCP() 
        local name = self:GetClass()
        
        if string.lower( string.sub(name, 1, 4) ) == "cp_" then
            return true
        else
            return false
        end
    end


function ENT:BehaveUpdate( fInterval ) --Important stuff here
	
	if ( !self.BehaveThread ) then
	return
	end
	--stuff goes past here
	--PrintTable(local_ents)
	
		--print(self.Enemy)
		local_ents = ents.FindInSphere(self:GetPos(), self.SearchRadius)
	for k,v in pairs(local_ents)do
	if v:IsCP() then
	self.loco:FaceTowards(v:GetPos())
	end
end

but once it gets to this point:


if v:IsCP() then
	self.loco:FaceTowards(v:GetPos())

it says that the method “IsCP()” is nil
anything wrong?

try

IsCP(v)

That would mean that IsCP is a global function.

Try


 if v.IsCP && v:IsCP() then 

This will check if IsCP is a function on v’s end and know wether or not it even is a CP.

You’re running this on every entity nearby, not just CPs. The above posted solution should do the job, assuming every entity you intended this to affect has the IsCP function.

I put the IsCP function in my CP bot, it still dosen’t work.

If i put,


if v.IsCP and v:IsCP() then
--carp
end

it prevents my bot from crashing and giving the error, but it won’t find anything nearby

dunno about your problem, but you could simplify this
[lua]
if string.lower( string.sub(name, 1, 4) ) == “cp_” then
return true
else
return false
end
[/lua]
to
[lua]
return string.lower( string.sub(name, 1, 4) ) == “cp_”
[/lua]

[editline]21st November 2014[/editline]

WAIT YES I KNOW WHAT THE PROBLEM IS
1, 3
not 1, 4
It would return cp_w if the name is cp_whatever