{CLOSED} AI killers chasing themselves instead of the players

  1. What do you want to achieve?
    I need to make that the AI killer bot chases only the player but not the other AI bots.

  2. What is the issue?
    This is a very common issue that the AI bots try to kill themselves when there are more then 1 of them.

  3. What solutions have you tried so far?
    I tried looking everywhere but couldnt find anything about this. And I’m a beginner to scripting.
    They just mostly do this instead of chasing the player.
    Capture

Thank you sm for helping!

1 Like

In order to help you, we’ll need to see the code that is currently being used.

I assume that the issue is because you aren’t checking what is being chased correctly though

Hello! Your NPCs seem to be looking for the closest Humanoid, regardless if it’s a player or not.
You can remedy this by specifically looking for Humanoids that are parented to players characters.

If you can post the code your NPCs are running off I’d be happy to edit it for you :slight_smile:

Hello, it is a weird code.
image

local SearchDistance = 10000 – How far a player can be before it detects you

local ZombieDamage = 25 – How much damage the Zombie inficts towards the player
local DamageWait = 2 – How many seconds to wait before it can damage the player again

local WanderX, WanderZ = 30, 30
– How many studs the zombie can wander on the x and z axis in studs ; 0, 0 to stay still

–[[


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


–]]

function getHumanoid(model)
for _, v in pairs(model:GetChildren())do
if v:IsA’Humanoid’ then
return v
end
end
end

local zombie = script.Parent
local human = getHumanoid(zombie)
local hroot = zombie.HumanoidRootPart
local zspeed = hroot.Velocity.magnitude
local head = zombie:FindFirstChild’Head’
local vars = script.vars

local pfs = game:GetService(“PathfindingService”)
local players = game:GetService(‘Players’)


local path
local waypoint

local chaseName = nil

function GetTorso(part)
local chars = game.Workspace:GetChildren()
local chaseRoot = nil
local chaseTorso = nil
local chasePlr = nil
local chaseHuman = nil
local mag = SearchDistance
for i = 1, #chars do
chasePlr = chars[i]
if chasePlr:IsA’Model’ and chasePlr ~= zombie then
chaseHuman = getHumanoid(chasePlr)
chaseRoot = chasePlr:FindFirstChild’HumanoidRootPart’
if chaseRoot ~= nil and chaseHuman ~= nil and chaseHuman.Health > 0 and chaseHuman.Name ~= “Zombie” then
if (chaseRoot.Position - part).magnitude < mag then
chaseName = chasePlr.Name
chaseTorso = chaseRoot
mag = (chaseRoot.Position - part).magnitude
end
end
end
end
return chaseTorso
end

function GetPlayersBodyParts(t)
local torso = t
if torso then
local figure = torso.Parent
for _, v in pairs(figure:GetChildren())do
if v:IsA’Part’ then
return v.Name
end
end
else
return “HumanoidRootPart”
end
end


local damagetime
local damagedb = false

for _, zambieparts in pairs(zombie:GetChildren())do
if zambieparts:IsA’Part’ and human.Health > 0 then
zambieparts.Touched:connect(function(p)
if p.Parent.Name == chaseName and p.Parent.Name ~= zombie.Name and not damagedb then – damage
damagedb = true
damagetime = time()
local enemy = p.Parent
local enemyhuman = getHumanoid(enemy)
vars.Attacking.Value = true
enemyhuman:TakeDamage(ZombieDamage)
vars.Attacking.Value = false
while wait() do
if damagetime ~= nil and time() >= (damagetime + DamageWait) then
damagedb = false
damagetime = nil
end
end
end
end)
end
end

– wandering
spawn(function()
while vars.Wandering.Value == false and human.Health > 0 do
vars.Chasing.Value = false
vars.Wandering.Value = true
local desgx, desgz = hroot.Position.x+math.random(-WanderX,WanderX), hroot.Position.z+math.random(-WanderZ,WanderZ)
local function checkw(t)
local ci = 3
if ci > #t then
ci = 3
end
if t[ci] == nil and ci < #t then
repeat ci = ci + 1 wait() until t[ci] ~= nil
return Vector3.new(1,0,0) + t[ci]
else
ci = 3
return t[ci]
end
end

	path = pfs:FindPathAsync(hroot.Position, Vector3.new(desgx, 0, desgz))
	waypoint = path:GetWaypoints()
	local connection;
	
	local direct = Vector3.FromNormalId(Enum.NormalId.Front)
	local ncf = hroot.CFrame * CFrame.new(direct)
	direct = ncf.p.unit
	local rootr = Ray.new(hroot.Position, direct)
	local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
	
	if path and waypoint or checkw(waypoint) then
		if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
			human:MoveTo( checkw(waypoint).Position )
			human.Jump = false
		end
		
		if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
			connection = human.Changed:connect(function()
				human.Jump = true
			end)
			human:MoveTo( waypoint[4].Position )
		else
			human.Jump = false
		end
		
		if connection then
			connection:Disconnect()
		end
		
	else
		for i = 3, #waypoint do
			human:MoveTo( waypoint[i].Position )	
		end
	end
	wait(math.random(4,6))
	vars.Wandering.Value = false
end

end)


while wait() do
local nrstt = GetTorso(hroot.Position)
if nrstt ~= nil and human.Health > 0 then – if player detected
vars.Wandering.Value = false
vars.Chasing.Value = true
local function checkw(t)
local ci = 3
if ci > #t then
ci = 3
end
if t[ci] == nil and ci < #t then
repeat ci = ci + 1 wait() until t[ci] ~= nil
return Vector3.new(1,0,0) + t[ci]
else
ci = 3
return t[ci]
end
end

	path = pfs:FindPathAsync(hroot.Position, nrstt.Position)
	waypoint = path:GetWaypoints()
	local connection;
	
	local direct = Vector3.FromNormalId(Enum.NormalId.Front)
	local ncf = hroot.CFrame * CFrame.new(direct)
	direct = ncf.p.unit
	local rootr = Ray.new(hroot.Position, direct)
	local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
	
	if path and waypoint or checkw(waypoint) then
		if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
			human:MoveTo( checkw(waypoint).Position )
			human.Jump = false
		end
		
		if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
			connection = human.Changed:connect(function()
				human.Jump = true
			end)
			human:MoveTo( waypoint[4].Position )
		else
			human.Jump = false
		end
		
		hroot.Touched:connect(function(p)
			local bodypartnames = GetPlayersBodyParts(nrstt)
			if p:IsA'Part' and not p.Name == bodypartnames and phit and phit.Name ~= bodypartnames and phit:IsA'Part' and rootr:Distance(phit.Position) < 5 then
				connection = human.Changed:connect(function()
					human.Jump = true
				end)
			else
				human.Jump = false
			end
		end)
		
		if connection then
			connection:Disconnect()
		end
		
	else
		for i = 3, #waypoint do
			human:MoveTo( waypoint[i].Position )	
		end
	end
	path = nil
	waypoint = nil
elseif nrstt == nil then -- if player not detected
	vars.Wandering.Value = false
	vars.Chasing.Value = false
	CchaseName = nil
	path = nil
	waypoint = nil
	human.MoveToFinished:Wait()
end

end

Hello bat, that format scares me more than the killers. Anyway, in the part of the code where your killer picks the target humanoid, add a condition that the target humanoid’s parent doesn’t share a name with the killer.

If target.Parent.Name ~= “Killer” then
– Rest of the code
end

Maybe alexi has a better idea, though.

Stayputnik

That is a great idea, I understand what you mean. But sadly Im not skilled enough to write that code.

Not with that attitude. Find the function and add that condition, it’s really no big deal. If you need more specific help, let me know and I’ll see what I can do.

Stayputnik

This is a really big script for PathFindingService.
I recommend you to skim through this iframe :

Hope you understand your script error with the help of above mentioned code.

Hello Xerto, it seems bat is using a free model and by his own admission doesn’t understand the code. I respect your resource, but this is too complex.

@qzb4t To avoid breaking your NPC, do not paste the script Xerto provided above. Stick with what I suggested and please report back when you’ve tried it out.

Stayputnik

I think I will try using a different free model, the one Im currently using is working but the script is too complicated and long.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.