How can i make Npc follows other Nearest Npc

I tried to make it myself:

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local Model = script.Parent
local Tool = script.Parent.ClassicSword
local closestNPC = nil
Tool.Use2:Fire()
local minDistance = 35
local pivot = Model:GetPivot()
for _, npc in ipairs(game.Workspace:GetChildren()) do
	if npc.Name == "Dummy" then
		local distance = (script.Parent.PrimaryPart.Position - npc.PrimaryPart.Position).Magnitude
		if distance < minDistance then
			minDistance = distance
			closestNPC = npc
			Model.Humanoid:MoveTo(closestNPC.PrimaryPart.Position)
			Tool.Use:Fire()
		end
	else
		return
	end
end

This didint worked

try this and let me know if it works

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local Model = script.Parent
local Tool = script.Parent.ClassicSword
local closestNPC = nil
Tool.Use2:Fire()
local minDistance = 35
local pivot = Model:GetPivot()
for _, npc in ipairs(game.Workspace:GetChildren()) do
	if npc.Name == "Dummy" then
		local distance = (script.Parent.PrimaryPart.Position - npc.PrimaryPart.Position).Magnitude
		if distance < minDistance then
			minDistance = distance
			closestNPC = npc
			
		end
	else
		return
	end
end
Model.Humanoid:MoveTo(closestNPC.PrimaryPart.Position)

I have a different, more optimized approach in mind for you.

Take everything you need from the humanoid’s properties, such as HP, MaxHP, movement, animations, and speed. Then, we will need to remove the humanoid from the char. To handle animations, simply insert an AnimationController into the rig, then insert an Animator into that. This will allow for animations to be made and ran within the model. After this, you will need to add a Configuration. Inside of it, add a NumberValue called “Health” and an integer value called “MaxHealth.” Set these equal to eachother, I presume 100 as you are using humanoids. Now add another NumberValue, name it “Speed.” This will determine the speed of your NPC. Then, you will need to add in 6 things into the HumanoidRootPart, this being 4 attachments, 1 AlignOrientation and 1 AlignPosition instance. Name all the 4 attachments accordingly (i.e. AlignOrientationCharacter, AlignOrientationGoal, AlignPositionCharacter, AlignPositionGoal.) This will allow us to reparent those attachments into another character, thus providing movement to the NPC. Once you have done this, insert a Script into the NPC and paste the following:

local minDistance = 35
cript.Parent.PrimaryPart.AlignPosition.MaxVelocity = script.Parent.Configuration.Speed.Value
for i, v in pairs(game.Workspace:GetChildren()) do
      if v.Name == "Dummy" then
            local distance = (script.Parent.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude
            if distance < minDistance then
                  minDistance = distance
                  closestNPC = v
                  script.Parent.HumanoidRootPart.AlignPositionGoal.Parent = closestNPC.PrimaryPart
                  script.Parent.HumanoidRootPart.AlignOrientationGoal.Parent = closestNPC.PrimaryPart
                  Tool.Use:Fire()
            end
      end
end
1 Like

dude that is way too long, don’t over do it

Here’s how I tried to do it. Let me know if it works or update me if you have any bugs or errors.
I haven’t added the sword part that was in your script - you can ask if you need help for that if the rest works properly.
Here I’ve only made it walk over to the closest dummy within the minimum distance of 35 studs.

local model = script.Parent
local modelPosition = model.PrimaryPart.Position
local function moveToNearestNpc()
	local closestNpc
	local minDistance = 35
	for _, npc in pairs(workspace:GetChildren()) do
		if npc.Name == "Dummy" then
			local distance = (modelPosition - npc.PrimaryPart.Position).Magnitude
			if distance <= minDistance then
				minDistance = distance
				closestNpc = npc
			end
		end
	end
	model.Humanoid:MoveTo(closestNpc.PrimaryPart.Position)
end
moveToNearestNpc()

20 lines versus 14 lines, mine has more simplicity and adds optimization. This isn’t too overcomplicated, after all, the result is able to handle more than humanoids. We use it for Paradigm-Shift TD for enemy movement, it’s really simple.

I didn’t mean the code. I meant the post you wrote, ain’t nobody gonna read all that

If you aren’t willing to devote time into reading a response with thought and effort put into it to at least evaluate the results you are given, you will never blossom into a good developer. Systems such as pathfinding can be extremely complex and are typically very wordy to explain.

I tried it by putting that script inside Npc model it didint worked

I tried everyting you said but there was always error

even i put everyting you said
image

I tried it it didint worked but i found why it doesn’t moves.
Main problem was Npc keep trying Move to Same position
i updated your script and it worked

local model = script.Parent
local modelPosition = model.PrimaryPart.Position
local function moveToNearestNpc()
	local closestNpc
	local minDistance = 35
	for _, npc in pairs(workspace:GetChildren()) do
		if npc.Name == "Dummy" then
			if npc == script.Parent or npc == nil then continue end
			local distance = (modelPosition - npc.PrimaryPart.Position).Magnitude
			if distance <= minDistance then
				minDistance = distance
				closestNpc = npc
				model.Humanoid:MoveTo(closestNpc.PrimaryPart.Position)
			end
		end
	end
	model.Humanoid:MoveTo(closestNpc.PrimaryPart.Position)
end
moveToNearestNpc()
1 Like

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