Im trying to make it so the npc path finds towards the player but its not moving…
local pfs = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
hrp.Anchored = false
plrname = game.Players:FindFirstChildOfClass("Player")
local plr = game.Workspace:FindFirstChild(plrname.Name)
script.Parent["FNaF Vent Crawl"]:Play()
print(plr)
local moving = false
plr.HumanoidRootPart:GetPropertyChangedSignal("Position"):Connect(function()
if moving == false then
local path = pfs:CreatePath()
path:ComputeAsync(hrp.Position, plr.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
human:MoveTo(waypoint.Position)
human.MoveToFinish:Wait(0.1)
moving = true
end
moving = false
end
end)
--human:MoveTo(plr.HumanoidRootPart.Position)
There’s a few little issues - for one your script might run before any player has joined the game - causing your plrname object to be nil, thus the plr object could be nil, and thus its RootPart.
Event when the plr object wasn’t nil - there’s still a chance the RootPart could be nil as well. So I’ve added a line to Wait for the RootPart to be instantiated.
There were little errors in your event names, such as the MoveToFinish event of the Humanoid, should be MoveToFinished
Also, for some reason I’m not sure of - GetPropertyChangedSignal(“Position”) nor the Changed event I tested seem to work against the Player’s RootPart - I guess this might be because the Player is the network owner of these objects, but I would say that is likely a Bug, so I suggest you write a bug report on that in a different topic. @zeuxcg
As for solutions, you can write a while loop for now that detects positional changes from the player’s travel distance.
local pfs = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
hrp.Anchored = false
wait(2) -- wait till there's at least one player.
local plrname = game.Players:FindFirstChildOfClass("Player")
local plr = game.Workspace:FindFirstChild(plrname.Name)
local plrRootPart = plr:WaitForChild("HumanoidRootPart")
script.Parent["FNaF Vent Crawl"]:Play()
print(plr)
local moving = false
local lastPos = plrRootPart.Position
while true do
wait(1)
local dist = (plrRootPart.Position-lastPos).Magnitude
if moving == false and dist > 2 then -- if the player has moved at least 2 studs - allow the path to be updated.
moving = true
lastPos = plrRootPart.Position
local path = pfs:CreatePath()
path:ComputeAsync(hrp.Position, plrRootPart.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
human:MoveTo(waypoint.Position)
human.MoveToFinished:Wait(0.1)
end
moving = false
end
end
This still isn’t a great solution - for example, if your player dies - this script won’t detect the new character yet.
I suggest looking into the PlayerAdded and CharacterAdded events and re-establishing your targets after the character dies - this will fix the issues you currently have with grabbing the plr and root part as well.