GoalI"m do the efforts to make a basic NPC that have the right to follow or walk come the player prefer a generic roblox zombie would.

You are watching: Roblox how to make an npc

ProblemI don"t understand which is the correct method to carry out it, mine script simply won"t work and also it"s not firing anything inside the function, that won"t also print. I"m targeting the player"s HRP and both the the player and also NPC use R15.

```lualocal football player = 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```

https://www.youtube.com/watch?v=pQ9Mcc-5UuA I just saw people talk around it i hope it help roblox made something called pathfinding and it automatically finds a path. The human in the video made part monsters. Turquoise_monkeyman 32 — 2y
https://web.roblox.com/games/168156797/Pathfinding is a uncopylocked video game using pathfinding turquoise_monkeyman 32 — 2y
I hope i helped. I know it didn"t answer your question however I hope it to be helpfu. Tell me if it to be turquoise_monkeyman 32 — 2y
https://ptcouncil.net/questions/77221/how-to-make-an-npc-following-script-with-radius#72450 hellmatic 1523 — 2y

1 answer

Log in come vote

Answered by
PhantomVisual 987

2 years back

The trouble is you are using :MoveTo() v the dorn parameters as. Your script only runs when as well and doesn"t begin the if loop.

Humanoid:MoveTo(location, part)

The an initial parameter, location, is a Vector3 value. This is wherein you want to humanoid come go. The second parameter, part, is type of prefer an balance out to the location. If you click the wiki page, you can see a gif that describes it better.

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

Note: The Humanoid will certainly timeout after 8 seconds of gift unable to with to destination. To avoid this, store resetting the timeout by to run the function again. More info ~ above the wiki page pointed out above.

Making the NPC Better

Now that we know the :MoveTo() function, what are some means we can use this come our NPC?

PathfindingServiceAs lot as ns would prefer to define this, i never have used this before. What that does is it creates a course for the NPC come follow based on the geometry of the map. Below is a accuse on the wiki page:https://developer.roblox.com/articles/Pathfinding

Chasing the Nearest PlayerUsing size to get the distance in between two clues in studs will help us recognize what player is closest come our NPC and we can use :MoveTo() to have the NPC chase them.

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

See more: Sorting Algorithm With Best Asymptotic Runtime Complexity, Which Sorting Algorithm Is Fastest In Java

Note: Make certain you apply the loop to store checking for the nearest player. You additionally need come keep applying :MoveTo() to keep the NPC from time out.