Nextbot issue.

Hey guys so I’ve been working on this Nextbot and I’ve got it to work to the point I wanted to, but when I got a friend to get on and try and he did the thing that toggles the nextbot to chase you, but they never do, they only chase me. I don’t really understand why was wondering if someone could help me out.

Shared.lua



AddCSLuaFile()
 
ENT.Base			= "base_nextbot"
 
function ENT:Initialize()




	self:SetModel( "models/Police.mdl" )
	self.Enemy=player.GetHumans()[1]
	self.Attacking=true
	self.Idling=true
	self.targetname=self:GetClass().."_"..tostring(self:EntIndex())
	self:SetKeyValue("targetname",self.targetname)
	self:SetHealth(40)
	self.WalkSpeed=50
	self.NextAmble=0
	
end
 
function ENT:Draw()
	self:DrawModel()

    local Pos = self:GetPos()
    local Ang = self:GetAngles()

    cam.Start3D2D(Pos+Vector(0,-6.5,-10) + Ang:Up() * 110, Angle( 0, 90, 90 ), 0.1)
	
		
		draw.WordBox(2, 0, 230, "Police Officer", "DermaLarge", Color(0, 0, 0, 100), Color(50, 50, 150, 255))	

    cam.End3D2D()
end

function ENT:EnemyInRange()

		return self.Enemy:GetPos():Distance(self:GetPos())<=80
end

function ENT:OnStuck(ent)
self:SetPos( self.Enemy:GetPos() )
end

function ENT:OnInjured(dmginfo) 
	self.Idling=false
end

function ENT:OnOtherKilled(dmginfo)
	self.Idling=false

end

function ENT:OnLandOnGround()
self:StartActivity( ACT_RUN )
 end

function ENT:ShouldChase(entity)
	return self.Attacking
end


function ENT:RunBehaviour()

self:StartActivity( ACT_IDLE )


	while ( true ) do
		
			if CurTime()>self.NextAmble and self.Enemy:GetNWInt("WantedLevel", 0 ) < 0 and self.Enemy:GetNWInt("Crime", 0 ) < 0 then
				self:StartActivity(ACT_WALK)
				self:MoveToPos( self:GetPos() + Vector( math.Rand( -1, 1 ), math.Rand( -1, 1 ), 0 ) * 200 )
				
				self.NextAmble=CurTime()
			end
		
		
		if self.Idling then
			self.WalkSpeed=50
			
			if CurTime()>self.NextAmble then
				self:StartActivity(ACT_IDLE)
				
				self.NextAmble=CurTime()+math.random(1,5)
			end
		elseif self.Attacking then
			self.WalkSpeed=350
		end
		self.loco:SetDesiredSpeed( self.WalkSpeed )
		if not self.Attacking then
			self.Idling=true
		else
			self.Idling=false
		end
		
		if self.Attacking == true then
			self:StartActivity( ACT_IDLE )
		end
		
		if self.Attacking == false then
			self:StartActivity( ACT_IDLE )
		end
		if IsValid(self.Enemy) and self.Enemy:GetNWInt("WantedLevel") > 0 or self.Enemy:GetNWInt("Crime") > 0 and not self.Idling then
			self:StartActivity(ACT_RUN)
			if self:ShouldChase(self.Enemy) then
				self.Attacking=true
				local maxageScaled=math.Clamp(self.Enemy:GetPos():Distance(self:GetPos())/1000,0.1,3)
				
				self:MoveToPos(self.Enemy:GetPos(),{draw=false,maxage=maxageScaled})
				self:CheckDoorOpen()
				if self:EnemyInRange() then
					if self.Enemy:Alive() then
					self:EmitSound( "npc/metropolice/vo/holditrightthere.wav" )
					end
					self:StartActivity(ACT_IDLE)
					coroutine.wait(0.2)
					
					

					if self:EnemyInRange() then
					local dmginfo=DamageInfo()	
						dmginfo:SetDamagePosition(self:GetPos()+Vector(0,0,50))
						dmginfo:SetDamage(0) --damageless atm
						dmginfo:SetDamageType(DMG_CLUB)
						dmginfo:SetAttacker(self)
						self.Enemy:TakeDamageInfo(dmginfo)
						if self.Enemy:Alive() then
						self.Enemy:Kill()
						self.Enemy:SetNWInt("Crime", 0 )
						self.Enemy:SetNWInt("WantedLevel", 0)
						end
					end
					coroutine.wait(0.5)
					self:StartActivity( ACT_IDLE )
					
				end
			else
				self.Attacking=false
				self:Remove()
			end
end
		
		
	
		coroutine.yield()
	end
 end

function ENT:CheckDoorOpen()
	local offset=Vector(0,0,50)
	local doorTrace={
		start=self:GetPos()+offset,
		endpos=self:GetPos()+(self:EyeAngles():Forward()*70)+offset,
		filter=self,
		mask=MASK_NPCSOLID
	}
	local doorTraceRes=util.TraceLine(doorTrace)
	if IsValid(doorTraceRes.Entity) and doorTraceRes.Entity~=NULL then
		if string.match(doorTraceRes.Entity:GetClass(),"door") then
			self:StartActivity(ACT_IDLE)
			coroutine.wait(1)
				if IsValid(doorTraceRes.Entity) then
					local door=doorTraceRes.Entity
					door:SetNotSolid(true)
					door:DrawShadow(false)
					door:SetNoDraw(false)
					door:EmitSound("npc/metropolice/vo/covermegoingin.wav" )
					coroutine.wait(1)
					door:EmitSound("physics/wood/wood_box_impact_hard3.wav" )
					doorTraceRes.Entity:Fire("OpenAwayFrom",self.targetname)
					doorTraceRes.Entity:Fire("Open",self.targetname)
					coroutine.wait(2)
					timer.Simple( 1, function() door:Fire("Unlock", "", 0) end )
					
							
				end
			coroutine.wait(0.1)
			self:StartActivity( ACT_RUN )
		end
	end
end


Thanks in Advance.


	self.Enemy=player.GetHumans()[1]

Can you guess why?

For fuck sakes lol.

I’m such a high life, thanks man.

[editline]27th July 2016[/editline]

But if I put player.GetAll() it messes up.


 attempt to call method 'GetNWInt' (a nil value)


Are you seriously trying to set your “Enemy Entity” variable to a table and then complaining it doesn’t work?

How am I complaining, I’m new to nextbot and I just want it to find anyone with that NW variable and chase em. that’s all, :\

I just got back into Lua too so I’m rusty af. ˢᵒʳʳʳʳʸ.

If you can’t extract what you did wrong from my previous message.

You are trying to use an Entity method on a table.

It’s like trying to attach a Lego wheel onto an incompatible Lego brick.

How would I make it search for the closet player with that NW int?

Make a function that returns a table of all players, then you calculate distance of every player from the nextbot. The one with the least distance will be returned from the function.

[editline]27th July 2016[/editline]


function ENT:FindClosestPlayer()
	PLYS = player.GetAll()
	closestPlayer = PLYS[1] -- set first player as closest
	for i=2,#PLYS do -- cycle through every player, start from 2 since we already have 1 up there
		if self:GetPos():Distance(PLYS*:GetPos()) < self:GetPos():Distance(closestPlayer:GetPos()) then -- if next player is closer, set him as the closest, and repeat until we run ut of players
			closestPlayer = PLYS*
		end
	end
	return closestPlayer
end

There is probably a better way to write this, but hell, it should be working. Then just put self.Enemy = self:FindClosestPlayer()