#AI pathfinder stops working

3 messages · Page 1 of 1 (latest)

prisma fernBOT
#

studio** You are now Level 3! **studio

solemn gorge
#

local pf = game:GetService("PathfindingService")
local plrs = game:GetService("Players")
local rs = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local npc = game.Workspace.Monsters:WaitForChild("Monster")
local hum = npc:WaitForChild("Humanoid")
local event = game.ReplicatedStorage.Events.Moster:WaitForChild("FinderEventTranslator")
local event2 = game.ReplicatedStorage.Events.Moster:WaitForChild("FinderEventTranslator2")
local event3 = game.ReplicatedStorage.Events.Moster:WaitForChild("Walking")
local HitBox = npc:WaitForChild("HixBox")
npc.PrimaryPart:SetNetworkOwner(nil)

local waypoints
local nextwaypointindex
local reachedconnection
local blockedconnection

local walkspeed = script:GetAttribute("WALKSPEED")
local sprintspeed = script:GetAttribute("SPRINTSPEED")

local canHear = false
local MaxDistanceThingy = false
local MaxDistance = 0
local CanDetect = true

if CanDetect == true then
event.OnServerEvent:Connect(function()
canHear = true
MaxDistanceThingy = true
end)

event3.OnServerEvent:Connect(function()
    canHear = true
    MaxDistanceThingy = false
end)

event2.OnServerEvent:Connect(function()
    task.wait(3)
    canHear = false
end)

end

local function getpath(dest)
local path = pf:CreatePath({
AgentHeight = 6;
AgentRadius = 3.5;
AgenCanJump = false;
AgentCanClimb = false;

    Costs = {
        Water = 100;
        DangerZone = math.huge
    }
})

path:ComputeAsync(npc.HumanoidRootPart.Position, dest.Position)

return path

end

#

local function FindTarget()
if MaxDistance == false then
MaxDistance = 50
else
MaxDistance = 30
end
local nearestTarget

for index, player in pairs(plrs:GetPlayers())do
    if player.Character then
        local target = player.Character
        local distance = (npc.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

        if distance < MaxDistance then
            nearestTarget = target
            MaxDistance = distance
        end
    end
end

return nearestTarget

end

local function walkto(dest)
local path = getpath(dest)

if path.Status == Enum.PathStatus.Success then 
    for i, wp in pairs(path:GetWaypoints()) do
        local target = FindTarget() 

        if target then
            npc.Humanoid.WalkSpeed = sprintspeed
            break
        else
            npc.Humanoid.WalkSpeed = walkspeed
            hum:MoveTo(wp.Position)
            hum.MoveToFinished:Wait()
        end
    end
else
    hum:MoveTo(dest.Position - (npc.HumanoidRootPart.CFrame.LookVector * 10))
end

end

local function patrol()
local wayp = workspace.TestingMap.WayPoints:GetChildren()
local RandomWayPoint = math.random(1, #wayp)
local target = FindTarget()

if canHear == false then
    walkto(wayp[RandomWayPoint])
else
    hum:MoveTo(target.HumanoidRootPart.Position) -----problem
    hum.MoveToFinished:Wait()
end

end

while true do
task.wait(0.1)
patrol()
end