How do I make a model chase a player?

Hello, I wish to make a PNG chase a player like in Nico’s Nextbots, but I do not know how to do it.

npc.Humanoid.Touched:Connect(function(touch)

	if game.Players:GetPlayerFromCharacter(touch.Parent) and not plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] then

		plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = true

		touch.Parent.Humanoid:TakeDamage(100)

		wait(1)

		plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = false	
	end
end)


while wait() do

	local plrs = game.Players:GetPlayers()

	local closestHRP

	for i, plr in pairs(plrs) do

		if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character.Humanoid.Health > 0 then

			local hrp = plr.Character.HumanoidRootPart

			local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude


			if not closestHRP then closestHRP = hrp end

			if (hrpOfNPC.Position - closestHRP.Position).Magnitude < distanceBetween then

				closestHRP = hrp

			end
		end
	end

	if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then npc.Humanoid:MoveTo(closestHRP.Position) end
end

Current Script ^
image
No errors in output. Any help? The model itself is not anchored either.

1 Like

There is an already tutorial about it on youtube.

1 Like

I tried it and sadly it did not work, it gives me an error that says attempt to index nil with ‘Position’.
image
The primary part is set to the humanoidrootpart and it still gives me this error.

1 Like

It should be Player.Character.HumanoidRootPart.Positon or Player.Character.PrimaryPart.Position (Primary part must be HumanoidRootPart)

I recommend the first one though. which is

 Player.Character.HumanoidRootPart.Positon

Now your distance variable should be this

local distance = (NPC.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
1 Like

Sorry, I’m new to scripting. Is it meant to look like this?

If so it gives me an error.
image

If I try the other way around,


It gives me this error.
image

1 Like

Try this

local distance = (NPC.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
1 Like

image
the character in question;
image


repeat task.wait(.2) until Npc:FindFirstChild("HumanoidRootPart")
local distance = (NPC:FindFirstChild("HumanoidRootPart").Position - player.Character.HumanoidRootPart.Position).Magnitude

image
?

I think character is specific to players only, and not to models like your Dummy

Here is the dummy in question

can anyone help please? I really need this for my game

  • Inside the model, you were trying to find out if the model found, was the name of the NPC or not. However, you forgot to add .Name in the line:
if player:FindFirstChild("Humanoid") and player.Name ~= NPC then
  • You could also add another line of argument in there, like finding out if the child found is a model:
if player:IsA("Model") and player:FindFirstChild("Humanoid") and player.Name ~= NPC then
  • Additionally, you could also use game:GetService("Players"), on the line above. This can be used, to find if the name of the model, is the name of a player in the server. Then find the Magnitude; (the downside of this is, is that it could require more scripting).

  • Finally, you have a script in the topic, and a different script in the “ObungaDummy” model. Which is the script that you are currently using for the model?

1 Like