GoalI"m make the efforts to make a straightforward NPC that can follow or walk to the player prefer a generic roblox zombie would.

You are watching: How to make an npc in roblox

ProblemI don"t know which is the correct means to carry out it, mine script simply won"t work and also it"s no firing anything within the function, it won"t even print. I"m targeting the player"s HRP and both of the player and NPC usage 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 simply saw civilization talk about it i hope it helps roblox do something dubbed pathfinding and it automatically finds a path. The person in the video made part monsters. Turquoise_monkeyman 32 — 2y
https://web.roblox.com/games/168156797/Pathfinding is a uncopylocked game using pathfinding turquoise_monkeyman 32 — 2y
I hope ns helped. I know it didn"t answer your question however I expect it to be helpfu. Phone call me if it to be turquoise_monkeyman 32 — 2y
https://julianum.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 earlier

The difficulty is you are applying :MoveTo() through the not correct parameters as. Her script only runs once as well and doesn"t start the if loop.

Humanoid:MoveTo(location, part)

The very first parameter, location, is a Vector3 value. This is where you desire to humanoid to go. The second parameter, part, is type of prefer an offset to the location. If girlfriend click the wiki page, you can see a gif that describes it better.

If ns were to happen a part with the location being Vector3.new(0,0,0), the Humanoid will certainly walk to the part"s location and will update as soon as the part"s place changes.

Note: The Humanoid will timeout after 8 secs of being unable to reach to destination. To avoid this, save resetting the timeout by to run the role again. An ext info top top the wiki page pointed out above.

Making the NPC Better

Now the we know the :MoveTo() function, what space some ways we can apply this come our NPC?

PathfindingServiceAs much as ns would favor to describe this, i never have used this before. What it does is it create a course for the NPC to follow based on the geometry of the map. Here is a indict on the wiki page:https://developer.roblox.com/articles/Pathfinding

Chasing the Nearest PlayerUsing size to gain the distance between two points in studs will help us recognize what player is closest come our NPC and also we can apply :MoveTo() to have the NPC follow them.

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

See more: Do Great Danes Have Webbed Feet : Meet The Super, Why Do Great Danes Have Webbed Feet

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