How to make an npc follow you

I have a game and i’m trying to make npc’s to follow you and when they are close enough they will attack you

5 Likes

Have a loop that checks through all of the player’s and considers if the distance from one of the player’s character’s parts to one of the npc’s character’s part’s has a distance less than or equal to some threshold where they are determined to be in “Attacking range”

This is a super popular question so you could look through free models to find how someone else’s zombie code works. Here is some code I just wrote up that you can base your own code off of though if you need guidance.

local Players = game:GetService("Players")

local UPDATE_TIME = 1 / 10
local ATTACKS_PER_SECOND = 1
local DAMAGE = 10

local NPC = script.Parent

local NPCRoot = NPC:WaitForChild("HumanoidRootPart")
local NPCHumanoid = NPC:WaitForChild("Humanoid")

local Attacking = false


local function TriggerDamageOnTouch(Part)
	if Attacking == false then
		Attacking = true

		local Humanoid = Part.Parent:FindFirstChild("Humanoid")

		if Humanoid then
			Humanoid:TakeDamage(DAMAGE)
		end

		wait(ATTACKS_PER_SECOND)
		Attacking = false
	end
end

-- make limbs hurt player
for i, Descendant in pairs(NPC:GetDescendants()) do
	if Descendant:IsA("BasePart") then
		TriggerDamageOnTouch(Descendant)
	end
end

while true do
	-- find closest player
	local CurrentPlayers = Players:GetPlayers()

	local ClosestRoot
	local ClosestDistance = math.huge

	for i, Player in pairs(CurrentPlayers) do
		local Character = Player.Character

		if Character then
			local PlayerRoot = Character:FindFirstChild("HumanoidRootPart")

			if PlayerRoot then
				local Distance = (NPCRoot.Position - PlayerRoot).Position

				if Distance < ClosestDistance then
					ClosestDistance = Distance
					ClosestRoot = PlayerRoot
				end
			end
		end
	end

	-- attack closest player
	if ClosestRoot then
		NPCHumanoid:MoveTo(ClosestRoot.Position)
	end

	wait(UPDATE_TIME)
end
1 Like

for some reason the npc kills itself with this code

Because you are not checking if the Part touching the limb’s are a Descendant of NPC. You can figure out how to do this yourself I’m sure with all of the code I have given you.

Call Part:IsDescendantOf(Part2) to determine whether Part is a Descendant of Part2 where Part and Part2 are your own variables–not literally “Part” and “Part2”

Edit: Not 100% sure if this code works. Never tested it.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local npc = script.Parent
local rootPart = npc.HumanoidRootPart
local humanoid = npc.Humanoid

rootPart.Touched:Connect(function(hit)
    local player = Players:GetPlayerFromCharacter(hit.Parent) -- find the player

    if player then -- check if the player was found
        player.Character.Humanoid.Health = 0 -- damage the player
    end
end)

while true do
    for _, player in pairs(Players:GetPlayers()) do -- loop through all the players
        local playerRoot = player.Character and player.Character:FindFirstChild("HumanoidRootPart") -- get the player's humanoidrootpart

        if playerRoot and (playerRoot.Position - rootPart.Position).Magnitude <= 25 then -- check if the player is near the npc
            humanoid:MoveTo(player.Character.HumanoidRootPart.Position) -- move the npc
            humanoid.MoveToFinished:Wait() -- wait until the npc has reached the player (not sure if i should use movetofinished)
            break -- break the loop so it doesn't follow a different player
        end
    end

    RunService.Heartbeat:Wait() -- in case no player is near the npc
end)
10 Likes