I want to make an AI to find the player at any time and location. The problem is, the AI doesn’t use it’s Pathfinding right. It tries to walk through a wall instead of walking around it…
Script:
local humanoid = script.Parent.Humanoid
local humanoidrootpart = script.Parent.HumanoidRootPart
local PathFindingService = game:GetService("PathfindingService")
local agentparams = {
AgentRadius = 8,
AgentHeight = 5,
AgentCanJump = true
}
local function getPath(destination)
local path = PathFindingService:CreatePath(agentparams)
path:ComputeAsync(humanoidrootpart.Position, destination.Position)
return path
end
local function getToPlayers()
local target
local players = game.Players:GetPlayers()
for _, plr in pairs(players) do
local char = plr.Character
if char then
local plrhumanoidrootpart = char:FindFirstChild("HumanoidRootPart")
if plrhumanoidrootpart then
if target then
if (humanoidrootpart.Position - plrhumanoidrootpart.Position).Magnitude < 200 then
target = plrhumanoidrootpart
end
else
target = plrhumanoidrootpart
end
end
end
end
if target then
local path = getPath(target)
for i, waypoint in pairs(path:GetWaypoints()) do
humanoid:ChangeState(Enum.HumanoidStateType.Running)
humanoid:MoveTo(waypoint.Position)
end
end
end
game:GetService("RunService").Stepped:Connect(getToPlayers)
I think your script is running through the waypoints too fast as it is not waiting for the first move command to finish before moving on to the next…
Change it to:
for i, waypoint in pairs(path:GetWaypoints()) do
humanoid:ChangeState(Enum.HumanoidStateType.Running)
humanoid:MoveTo(waypoint.Position)
humanoid:MoveToFinished:Wait()
end
local humanoid = script.Parent.Humanoid
local humanoidrootpart = script.Parent.HumanoidRootPart
local PathFindingService = game:GetService("PathfindingService")
local agentparams = {
AgentRadius = 8,
AgentHeight = 5,
AgentCanJump = true
}
local function getPath(destination)
local path = PathFindingService:CreatePath(agentparams)
path:ComputeAsync(humanoidrootpart.Position, destination.Position)
return path
end
local function getToPlayers()
local target
local players = game.Players:GetPlayers()
for _, plr in pairs(players) do
local char = plr.Character
if char then
local plrhumanoidrootpart = char:FindFirstChild("HumanoidRootPart")
if plrhumanoidrootpart then
if target then
if (humanoidrootpart.Position - plrhumanoidrootpart.Position).Magnitude < 200 then
target = plrhumanoidrootpart
end
else
target = plrhumanoidrootpart
end
end
end
end
if target then
local path = getPath(target)
for i, waypoint in ipairs(path:GetWaypoints()) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:ChangeState(Enum.HumanoidStateType.Running)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
while true do
getToPlayers()
task.wait()
end
The waypoint, is reset every .5 seconds so it looks like the npc isn’t constantly aware of it’s targets position. Would you know how to make the npc chase the targets position without any breaks?