Hi. I am making an AI like piggy, and it works just fine but its body is just kinda shaking, and sometimes it goes to the old position of the player and then go to the new position. Here’s a video:
https://gyazo.com/8aa9e5f07a4bbcc97f9df8f4afabe56d
and here’s my code:
local noob = script.Parent
local human = script.Parent:WaitForChild("Humanoid")
local rootpart = script.Parent:WaitForChild("Torso")
local runservice = game:GetService("RunService")
local pathfindingservice = game:GetService("PathfindingService")
local paths = game.Workspace:WaitForChild("Paths"):GetChildren()
local dist = 200
local function getDistance(humanoidRoot, targetPosition)
return (humanoidRoot.Position - targetPosition).magnitude
end
local function moveToFinished(root, target)
repeat runservice.Stepped:Wait() until (rootpart.Position - target).magnitude <= 5
end
local function returnWaypointsAndCreatePath(startPoint, endPoint)
local path = pathfindingservice:CreatePath()
path:ComputeAsync(startPoint, endPoint);
return path:GetWaypoints()
end
local function setNetworkOwner(npc)
for i, v in pairs(npc:GetChildren()) do
if v:IsA("BasePart") then
v:SetNetworkOwner(nil)
end
end
end
local function findTarget()
for index, target in pairs(workspace:GetChildren()) do
if target:IsA("Model") and target:FindFirstChild("Humanoid") and target.Name ~= noob.Name and (target.PrimaryPart.Position - rootpart.Position).Magnitude <= dist then
dist = (target.PrimaryPart.Position - rootpart.Position).Magnitude
return (target.PrimaryPart);
else
dist = 200;
end
end
return "random", Vector3.new(math.random(-50, 50),0, math.random(-50, 50))
end
local function followPath(startPoint, endPoint)
local waypoints = returnWaypointsAndCreatePath(startPoint, endPoint)
for index, point in pairs(waypoints) do
if point.Action == Enum.PathWaypointAction.Jump then
human.Jump = true
end
human:MoveTo(point.Position);
local distance = getDistance(rootpart, waypoints[index].Position)
if distance >= 10 then --If the player's distance is greater than 10, break the for loop so we can recalculate a path.
break
end
--Wait for them to reach the position if the player is still close:
moveToFinished(rootpart, waypoints[index].Position)
end
end
setNetworkOwner(noob)
while wait() do
local target, vector = findTarget()
if target ~= "random" and vector == nil then
followPath(rootpart.Position, target.Position)
else
followPath(rootpart.Position, rootpart.Position+vector);
end
end
Help pls. ![]()