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 ^
No errors in output. Any help? The model itself is not anchored either.
I tried it and sadly it did not work, it gives me an error that says attempt to index nil with ‘Position’.
The primary part is set to the humanoidrootpart and it still gives me this error.
repeat task.wait(.2) until Npc:FindFirstChild("HumanoidRootPart")
local distance = (NPC:FindFirstChild("HumanoidRootPart").Position - player.Character.HumanoidRootPart.Position).Magnitude
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?