attempt to call method "IsPlayer" - aNOTHER nil value.

I’m creating an animal SNPC pack, so far, the goat is finished (aside from idle sound repetition), the rat (I’d say) is pretty much finished (ditto - goat)
I’m trying to make the dog spawn a DamageInfo at the player’s position whenever a player attacks it (this was a test on whether the DamageInfo would work), in this case, it didn’t, it says GetPos() returns a nil value.


[LUA]AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
 
include('shared.lua')

ENT.Model = "models/npc/dog/npc_dog.mdl"
ENT.Classify = CLASS_EARTH_FAUNA
ENT.PlayerEnemy = false
ENT.Idle1 = Sound"npcs/dog/dog_misc0.wav"
ENT.Idle2 = Sound"npcs/dog/dog_misc1.wav"
ENT.Idle3 = Sound"npcs/dog/dog_misc2.wav"
ENT.Death1 = Sound"npcs/dog/dog_dying0.wav"
ENT.Death2 = Sound"npcs/dog/dog_dying1.wav"
ENT.Death3 = Sound"npcs/dog/dog_dying2.wav"
ENT.Pain1 = Sound"npcs/dog/dog_ouch_strong0.wav"
ENT.Pain2 = Sound"npcs/dog/dog_ouch_strong1.wav"
ENT.Pain3 = Sound"npcs/dog/dog_ouch_strong2.wav"



function ENT:Initialize()
	self:SetModel( self.Model )
	self:SetHullType( HULL_HUMAN )
	self:SetHullSizeNormal()
	self:SetSolid( SOLID_BBOX )
	self:SetMoveType( MOVETYPE_STEP )
	self:CapabilitiesAdd( CAP_MOVE_GROUND , CAP_INNATE_MELEE_ATTACK1)
	self:SetMaxYawSpeed( 300 )
	self:SetHealth(250)
end

function ENT:OnTakeDamage(dmg)
self:SetHealth( self:Health() - dmg:GetDamage())
local target = dmg:GetAttacker()
if target:IsPlayer() then
self.PlayerEnemy = true
    if self:Health() <= 0 then
local rdoll = ents.Create("prop_ragdoll")
local pos = self:GetPos()
rdoll:SetModel(self.Model)
rdoll:SetPos(pos)
rdoll:Spawn()
self:Remove()
local DeathSelect = math.random(1,3)
if DeathSelect == 1 then
self:EmitSound(self.Death1)
else
if DeathSelect == 2 then
self:EmitSound(self.Death2)
else
if DeathSelect == 3 then
self:EmitSound(self.Death3)
					end
				end
			end
		end
	end
end

function ENT:Think()
local bitedmgscale = math.random(1,3)
local pos = self:GetPos()
local enemyget = ents.FindInSphere( pos, 100 )
if self.PlayerEnemy == true then
	for k, v in pairs ( enemyget ) do
	local enemyisplayer = v:IsPlayer()
	if enemyisplayer == true then
	local bitedmg = DamageInfo()
	local pos = enemyget:GetPos()
	bitedmg:SetDamageType(DMG_CLUB)
	bitedmg:SetInflictor(self)
	bitedmg:SetDamagePosition(pos)
	if bitedmgscale == 1 then
	bitedmg:SetDamage(11)
	else
	if bitedmgscale == 2 then
	bitedmg:SetDamage(31)
	else
	if bitedmgscale == 3 then
	bitedmg:SetDamage(62)
						end
					end
				end
			end
		end
	end
end

[/LUA]

Here’s the error:

This means (from what i can tell) that the player WAS found in the sphere of 100 units, but GetPos is returning nil for an unknown reason.

I will implement the code to have the dog walk to the player to deal the damage in time, but for now, it’s a test.

What I need are:
A answer for the SetPos() error.
An idle sound repeater (I tried an infinite timer that plays idle sounds every 10s but it still returns fucking nil >:|)

IsPlayer in the title was an accident btw…

First thing, thats a very painful strain on the server calling all that, every single think.

And im not sure, but it may be due to the doubling of pos. Yes they are both local but i dont know how it works with if statements inside one another, i recommend changing one of their names.

change
[lua]local pos = enemyget:GetPos()[/lua]
to
[lua]
local pos = v:GetPos()
[/lua]

edit:

And consider learning to nest your code properly. It’s hard to see what code is inside of what if statement.

Ah, thanks… it removed the error, but attack damage is’nt being dealt, i’ll try and find out how to get that to work…

Now whenever you attempted to call a method that is nil, double check your object and don’t make a thread over it.