Trying to make an NPC follow the nearest player, but for some reason it just sits there and refuses to come down? It’ll go up just fine, but I can only get it to go down if I guide it down the ramp.
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local path = PathfindingService:CreatePath({
WaypointSpacing = math.huge
})
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local function followPath(destination)
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
followPath(destination)
end
end)
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
end
end
wait(5) --Let player spawn
local prevpos = character.PrimaryPart.Position --Jump if stuck
for count = 1, math.huge do --Loop forever so it updates nearest player position, for loop to detect first iteration
if character.PrimaryPart.Position == prevpos then
humanoid.Jump = true
end
prevpos = character.PrimaryPart.Position
local chars = {}
for i, v in pairs(Players:GetPlayers()) do
if v.Character then
chars[v.Name]=v:DistanceFromCharacter(character.PrimaryPart.Position)
end
end
local closest = math.huge
local target
for i, v in pairs(chars) do
if v < closest then
closest = v
target = Players:FindFirstChild(i)
end
end
if count ~= 1 then
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
followPath(target.Character.PrimaryPart.Position)
end
I wish it were as simple as making it just go to the position of the nearest player and jump if stuck, but any remotely complex environment would prevent that approach from working sadly.
Edit: I’m also pretty inexperienced with pathfinding, so I might not understand some things.