Alright, so I am trying to make a pathfinding script where the enemy chases the player when in a certain radius. However, the enemy will get stuck on walls when chasing the player. The enemy (when not chasing the player) goes to a random part in a folder called waypoints. when it is going to a random part, it works perfectly fine, however, when I tried to copy that bit over into the chase part, it just broke the script.
local enemy2 = script.Parent
local humanoid = enemy2.Humanoid
local PathFindingService = game:GetService(“PathfindingService”)
enemy2.PrimaryPart:SetNetworkOwner(nil)
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 30
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (enemy2.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 6,
["AgentRadious"] = 9,
["AgentCamJump"] = false
}
local path = PathFindingService:CreatePath(pathParams)
path:ComputeAsync(enemy2.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (enemy2.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 6 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local attackAnim = humanoid:loadAnimation(script.Attack)
attackAnim:Play()
target.Humanoid.Health = (target.Humanoid.Health - 10)
wait(1)
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
print("TARGET FOUND!", target.Name)
attack(target)
enemy2.Humanoid.WalkSpeed = 17
break
else
print("Moving to ", waypoint.Position)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(0.1)
enemy2.Humanoid.WalkSpeed = 8
end
end
else
humanoid:MoveTo(destination.Position - (enemy2.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function wander()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait(0) do
wander()
end
Anyone able to help me fix this?