Whether this is a bug on my own behalf or Roblox’s engine being weird, for some reason my npc script is behaving really strangely, walking into walls and just standing there staring at objects it can as easily walk over, even though as far as I know I’ve configured it right.
From the image below it seems to have calculated a good path but is choosing not to use it? I dont’ really know how PathfindingService works because I’ve only recently needed it.
(The dots marking the correct path isnt my code, I turned on Navigation Mesh and what have you.)
local PathfindingService = game:GetService("PathfindingService")
local players = game:GetService("Players")
local npc = script.Parent
local hrp = npc:WaitForChild("HumanoidRootPart")
local humanoid = npc:WaitForChild("Humanoid")
local path = nil
local waypoints = nil
local range = 50
local alive = true
local function CreatePlayerPath(target)
local PlayerPath = PathfindingService:CreatePath({
AgentCanJump = true,
AgentCanClimb = true,
Costs = {
UnwalkableZone = 100000,
Jump = 3,
Climb = 2
}
})
local targetHRP = target.Character:FindFirstChild("HumanoidRootPart")
local success, errorMessage = pcall(function()
PlayerPath:ComputeAsync(hrp.Position, targetHRP.Position)
end)
if not success or PlayerPath.Status ~= Enum.PathStatus.Success then
return nil
end
return PlayerPath
end
local function GetClosestPlayer()
local magnitudes = {}
local target = nil
for _, player in pairs(players:GetPlayers()) do
local dist = player:DistanceFromCharacter(hrp.Position)
if dist <= range then
table.insert(magnitudes, {dist, player})
end
end
table.sort(magnitudes, function(a, b)
return a[1] < b[1]
end)
if #magnitudes > 0 then
target = magnitudes[1][2]
end
return target
end
humanoid.Died:Connect(function()
alive = false
end)
task.wait(1)
local attackAnim = humanoid:LoadAnimation(script.AttackAnim)
while alive do
local target = GetClosestPlayer()
if target ~= nil then
local path = CreatePlayerPath(target)
if target.Character:WaitForChild("HumanoidRootPart") then
local mag = (target.Character:WaitForChild("HumanoidRootPart").Position - hrp.Position).Magnitude
if mag < 2 and script.Parent.HumanoidRootPart.Anchored == false then
attackAnim:Play()
if target.Character:FindFirstChild("ForceField") == nil and target.Character:WaitForChild("HumanoidRootPart").Anchored == false then
target.Character.Humanoid.Health = target.Character.Humanoid.Health - script.Damage.Value
end
task.wait(1)
end
end
if path then
waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
end
end
end
task.wait(0.3)
end
Is this Roblox being strange or this there something I’m missing?
