Okay so i’m currently making an NPC that will follow the nearest player in it’s proximity by using PathFindingService and its working alright-ish but i want it to be smooth.
now currently i just have a a function that checks whoever is the nearest player and i have another function that uses the pathfinding service that calls it and then looks at the coordinates then follows it, problem is that it just goes to the previous calculated position so its not a constant movement it stops in between.
now i’ve tried ways to make it quicker but i have failed it either stops moving at all or it wont move. i was looking into ray casting but i couldn’t figure it out how to get it to work
local function followNearestPlayer()
local nearestPlayer = getNearestPlayer()
if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
-- If a player is found, set walk speed to normal and follow
human.WalkSpeed = normalWalkSpeed
local targetPosition = nearestPlayer.Character.HumanoidRootPart.Position
local path = PathfindingService:CreatePath()
path:ComputeAsync(torso.Position, targetPosition)
local waypoints = path:GetWaypoints()
path.Blocked:Connect(function()
print("Blocked")
end)
for i, waypoint in waypoints do
--[[ local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position + Vector3.new(0, 2, 0)
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace]]
if waypoint.Action == Enum.PathWaypointAction.Jump then
human:ChangeState(Enum.HumanoidStateType.Jumping)
end
human:MoveTo(waypoint.Position)
human.MoveToFinished:Wait(1)
-- part:Destroy()
print("Moving to position")
end
human:MoveTo(targetPosition)
else
-- If no player is found, set walk speed to 0
human.WalkSpeed = 0
print("No players nearby, NPC stops.")
end
end
(Maybe important to add, i have 2 other scripts on the NPC, one handles it attacking and another handles animation based on the speed of the NPC)