Making an NPC follow the player and attack enemy NPCs at the same time

I have an NPC who follows the player’s character and attacks it, but now I want to create a friendly NPC who follows the player’s character, and if an NPC branded as an enemy is nearby, the friendly NPC will then follow the enemy and attack it. Should the enemy be killed, the friendly NPC will then resume following the player.

I, however, am unaware of how to let the NPC follow the player and then engage the enemy without alternating uncontrollably between both actions, or any other form of unwanted behaviour. I would like to know how I could create this friendly NPC, preferably using the code I already have for the hostile NPC.

Here is said code:

local players = game:GetService("Players")
local sets = script.Parent.Settings
local shouldStop = sets.ProximityStop
local stopStudAmount = shouldStop.ProximityValue.Value
local hrp = script.Parent.HumanoidRootPart
local anim = script.Parent.Humanoid:LoadAnimation(script:FindFirstChildOfClass("Animation"))
local damage = script.Parent.Damage.Script
local humanoid = script.Parent.Humanoid
local ogspeed = humanoid.WalkSpeed

local canattack = true

local function Follow()
	local distance = sets["Maximum Distance"].Value
	for _, player in ipairs(players:GetPlayers()) do
		local char = player.Character or player.CharacterAdded:Wait()
		local target = char:FindFirstChild("HumanoidRootPart")
		if target then
			if (target.Position - hrp.Position).Magnitude <= distance then
				script.Parent.Humanoid:MoveTo(target.Position, target)
				if (hrp.Position - target.Position).Magnitude < stopStudAmount and shouldStop.Value and canattack then
					canattack = false
					humanoid.WalkSpeed = 0
					anim:Play()
					task.wait(0.2)
					damage.Enabled = true
					task.wait(0.3)
					humanoid.WalkSpeed = ogspeed
					damage.Enabled = false
					canattack = true
				end
			end
		end
	end
end

while task.wait(0.2) do
	Follow()
end
2 Likes

Bumping the post. Hopefully someone will have an idea.

Even a day later, I still need help. I can’t find anything on making an NPC that is allied to the player, rather than hostile.

You can use collectionservice to tag hostile NPCs and loop through that instead of the players in game

I think I’m lining up your code with your description alright, besides the part about targeting enemies and friendlies. I’m certain that the uncontrollable, alternating behavior is due to that for-loop never breaking after finding a character within chasing distance.

Before introducing further complexities, there’s some refactoring opportunities to bring your goal closer.
Functions should perform one job only, so splitting the logic from Follow into their own functions will allow you to properly manage friendly and hostile behaviors. (Follow() GetEnemyWithinRange() DoAnimation(), etc)

I recommend starting again with just the NPC following your character after these changes, and then from there, you can add logic for switching between follow targets. You can edit your post with the updated code, and then we can further help you with your goal

Okay. I’ll work on it. It will probably take a while.

1 Like