-
What do you want to achieve? i want to make my ai chase script smooth, not delayed ;-;
-
What is the issue? My npc is waiting until path is finished, but i want to upgrade the path when target’s position changed, how to do it?
btw i captured a video for it: video -
What solutions have you tried so far? I didnt found a solution for it in devforum, also i tried many solutions like when target’s position changed, then break the loop, but its breaks loop so its not moving to waypoints when position changed, otherwise its moves to waypoints.
local soldier = script.Parent
local humanoid = soldier.Humanoid
local pathfindingservice = game:GetService("PathfindingService")
soldier.PrimaryPart:SetNetworkOwner(nil)
local function createPath(destination)
local pathfindingservice = game:GetService("PathfindingService")
local path = pathfindingservice:CreatePath()
path:ComputeAsync(soldier.HumanoidRootPart.Position, destination.HumanoidRootPart.Position)
-- this is showing path waypoints so nvm it
for i, w in pairs(workspace.waypoints:GetChildren()) do
w:Destroy()
end
local waypoints = path:GetWaypoints()
for i, w in pairs(waypoints) do
local waypoint = Instance.new("Part")
waypoint.Name = tostring(table.find(waypoints,w))
waypoint.Position = w.Position
waypoint.CanCollide = false
waypoint.Parent = workspace.waypoints
waypoint.Shape = "Ball"
waypoint.Size = Vector3.new(1, 1, 1)
waypoint.Anchored = true
end
return path
end
local function walkTo(destination)
local path = createPath(destination)
local waypoints = path:GetWaypoints()
for i, w in pairs(waypoints) do
humanoid:MoveTo(w.Position)
humanoid.MoveToFinished:Wait()
end
end
local function findTarget()
local targets = workspace.RedTeamSoldiers:GetChildren()
local maxDistance = math.huge
local nearestTarget
if not (targets == nil) then
for i, t in pairs(targets) do
local distance = (soldier.HumanoidRootPart.Position - t.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and t.Humanoid.Health > 0 then
nearestTarget = t
end
end
else
nearestTarget = nil
end
return nearestTarget
end
local function patrol()
local target = findTarget()
if target then
walkTo(target)
end
end
while true do
patrol()
end