I am currently making an RPG like game, where players attack NPCs for coins. I have a pathfinding script that will allow the NPC to move towards the player. The problem is, actually there are a few problems, let me list them down.
1.The NPC can never outrun the player, no matter the walkspeed 2.The NPC slows down as it approaches the player 3.As it gets near the player, the movement gets choppy, and looks likes its teleporting.
I tried many solutions, but they didn't help. Here is my code, if anybody knows a fix, please write down an answer.
local npc = script.Parent
local npcRoot = npc:FindFirstChild('HumanoidRootPart')
local humanoid = npc:FindFirstChild('Humanoid')
local pathfindingService = game:GetService('PathfindingService')
local maximumRange = math.huge
local function moveToDestination()
for _, plr in pairs(game.Players:GetPlayers()) do
if plr and plr.Character and plr.Character.Humanoid.Health > 0 then
local character = plr.Character
local humanoidRoot = character:FindFirstChild('HumanoidRootPart')
if humanoidRoot and (npcRoot.Position-humanoidRoot.Position).Magnitude <= maximumRange then
local path = pathfindingService:CreatePath()
path:ComputeAsync(npcRoot.Position, humanoidRoot.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping, true)
end
humanoid:MoveTo(waypoint.Position)
end
end
end
end
end
end
while true do
wait(0.1)
moveToDestination()
end
