Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

NPC Only moves to where the player is, not to the part I told it to go to?

Asked by
2_MMZ 1059 Moderation Voter
3 years ago

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.

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

0
waypoint on line 14 is higlighted in red, unless you meant waypoints instead? But however, trying both of these, I get two different error outputs. If I use waypoints, I get "Argument 1 missing or nil" if I use just waypoint, as in your script, I get "ServerScriptService.Script:20: attempt to index nil with 'Position' 2_MMZ 1059 — 3y
0
Oops, sorry. I fixed it. Gmorcad12345 434 — 3y
0
I'll try it out. 2_MMZ 1059 — 3y
0
This still did not work. My other script where it has MoveTo did make it move, but not this one. Everything in the NPC is unanchored. 2_MMZ 1059 — 3y
0
Also note, that I'm using a BindableEvent. You can learn more about my script here: https://scriptinghelpers.org/questions/123692/npc-wont-move-to-the-position-i-want-it-to 2_MMZ 1059 — 3y
Ad

Answer this question