Hello reader!
I’m trying to make a pathfinding AI, and so far I kinda got what I wanted, but the rig’s movement is very inconsistent, despite the path being found for a lack of better words. I want it to smoothly pathfind towards the player. If theres a better method than “Humanoid:MoveTo()” I would love to know.
The code:
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
local PathFindingService = game:GetService("PathfindingService")
local RootPart = script.Parent.HumanoidRootPart
local Humanoid = script.Parent.Humanoid
local Pathfinding = PathFindingService:CreatePath()
local function GetClosestPlayer()
local RootPosition = RootPart.Position
local distance = math.huge
local closestPlayer = nil
for _, v in pairs(PlayerService:GetChildren()) do
local character = v.Character
if character then
local HRP = character:FindFirstChild("HumanoidRootPart")
if HRP then
local PlayerPos = HRP.Position
local DistFromEntity = (RootPosition - PlayerPos).magnitude
if DistFromEntity < distance then
distance = DistFromEntity
closestPlayer = v
end
end
end
end
return closestPlayer, distance
end
RunService.Heartbeat:Connect(function()
local player, distance = GetClosestPlayer()
print(distance)
if player and distance < 100 then
local character = player.Character or player.CharacterAdded:Wait()
print(player.Name)
Pathfinding:ComputeAsync(RootPart.Position, character:FindFirstChild("HumanoidRootPart").Position)
local Points = Pathfinding:GetWaypoints()
if Pathfinding.Status == Enum.PathStatus.Success then
for _, v in pairs(Points) do
local Part = Instance.new("Part")
Part.Position = v.Position
Part.Anchored = true
Part.CanCollide = false
Part.Size = Vector3.new(5,5,5)
Part.Shape = Enum.PartType.Ball
Part.Parent = game.Workspace
Part.Color = Color3.fromRGB(170,0,170)
task.wait(0.1)
Part:Destroy()
end
for _, Point in pairs(Points) do
Humanoid:MoveTo(Point.Position)
end
end
end --the end for the first if statement
end)
The first part of the code is just to track the closest player. The second part is the pathfinding (it also has an extra part I added to visualise the pathfinding)
Video: