Hello. I'm making a game with NPCs. I figured out how to make some move, but now, they don't go to the part I want them to. There are ZERO SCRIPTS
that make them detect the player. I inserted a Dummy from the rig builder, unanchored everything, and customized it. The only script that makes them move is the script that includes the MoveTo()
. I need some help on this.
Any working answer is appreciated.
Do you have any script samples?
Do you want it to go to BOTH the player and the part?
The script below allows the NPC to go to the part with the use of :MoveTo()
local PFS = game:GetService("PathfindingService") local NPC = workspace.NPC --replace with your npc local humanoid = NPC:FindFirstChild("Humanoid") local destination = workspace.Destination --replace with the part that you want to NPC to go to local path = PFS:CreatePath() path:ComputeAsync(NPC.HumanoidRootPart.Position,destination.Position) local waypoints = path:GetWaypoints() for i, v in pairs(waypoints) do humanoid:MoveTo(v.Position) humanoid.MoveToFinished:Wait() end
If you want them to detect a player as well, replace the NPC with your character.
If you want them to detect the nearest player, use Magnitude and a while true do
loop
to constantly search for the nearest player. Then, compute the path and move their humanoid to the waypoints.
Here's the Pathfinding article that you might find useful