GoalI"m trying to make a basic NPC that can follow or walk to the player like a generic roblox zombie would.

You are watching: How to make npcs in roblox

ProblemI don"t know which is the correct way to do it, my script just won"t work and it"s not firing anything inside the function, it won"t even print. I"m targeting the player"s HRP and both of the player and NPC use R15.

```lualocal Players = game:GetService("Players")

for _, player in pairs(Players:GetPlayers()) dospawn(function()while true doprint("im smart")local pos = player.Character:FindFirstChild("HumanoidRootPart").Positionlocal pos2 = player.Character:FindFirstChild("HumanoidRootPart")script.Parent.Humanoid:MoveTo(pos, pos2)wait(0.1)endend)end```


0
https://www.youtube.com/watch?v=pQ9Mcc-5UuA I just saw people talk about it I hope it helps roblox made something called pathfinding and it automatically finds a path. The person in the video made some monsters. turquoise_monkeyman 32 — 2y
0
https://web.roblox.com/games/168156797/Pathfinding is a uncopylocked game using pathfinding turquoise_monkeyman 32 — 2y
0
I hope I helped. I know it didn"t answer your question but I hope it was helpfu. Tell me if it was turquoise_monkeyman 32 — 2y
0
https://les-grizzlys-catalans.org/questions/77221/how-to-make-an-npc-following-script-with-radius#72450 hellmatic 1523 — 2y

1 answer


Log in to vote
2
*
*

Answered by
*
PhantomVisual 987
*

2 years ago

The problem is you are applying :MoveTo() with the wrong parameters as. Your script only runs once as well and doesn"t start the while loop.

Humanoid:MoveTo(location, part)

The first parameter, location, is a Vector3 value. This is where you want to humanoid to go. The second parameter, part, is sort of like an offset to the location. If you click the wiki page, you can see a gif that explains it better.

If I were to pass a part with the location being Vector3.new(0,0,0), the Humanoid will walk to the part"s location and will update when the part"s position changes.

Note: The Humanoid will timeout after 8 seconds of being unable to reach to destination. To avoid this, keep resetting the timeout by running the function again. More info on the wiki page mentioned above.

Making the NPC Better

Now that we understand the :MoveTo() function, what are some ways we can apply this to our NPC?

PathfindingServiceAs much as I would like to explain this, I never have used this before. What it does is it creates a path for the NPC to follow based on the geometry of the map. Here is a tutorial on the wiki page:https://developer.roblox.com/articles/Pathfinding

Chasing the Nearest PlayerUsing Magnitude to get the distance between two points in studs will help us determine what player is closest to our NPC and we can apply :MoveTo() to have the NPC chase them.

local npcHRP = NPC.HumanoidRootPartlocal function GetNearestPlayer(minimumDistance) local closestMagnitude = minimumDistance or math.huge --minimumDistance is a number in studs local closestPlayer for i,v in next, game.Players:GetPlayers() do local Character = v.Character if (Character) then local humanoid = Character.Humanoid local HRP = Character.HumanoidRootPart if (humanoid.Health > 0) then local mag = (npcHRP.Position - HRP.Position).Magnitude if (mag Using this function, we get the nearest PlayerObject. We can "convert" that to a Vector3 and run the :MoveTo() function with it.

See more: How Many Feet Is 115 Inches In Feet? What Is 115 Inches In Feet

Note: Make sure you apply the loop to keep checking for the nearest player. You also need to keep applying :MoveTo() to keep the NPC from timing out.