#Im getting an error in my AI pathfinder for a monster

13 messages · Page 1 of 1 (latest)

quasi osprey
#

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
MaxDistance = 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 MaxDistanceThingy == true then
MaxDistance = 30
else
MaxDistance = 5
end
print(MaxDistance)
local NearestTarget

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

        if distance < MaxDistance and canHear == true 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
    CanDetect = false
    task.wait(0.1)
    hum:MoveTo(target.HumanoidRootPart.Position)
    hum.MoveToFinished:Wait()
    task.wait(0.1)
    CanDetect = true
end

end

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

dark ember
quasi osprey
#

Im getting an error in my AI pathfinder for a monster

dark ember
#

also could you describe the actual issue

#

like is the npc not moving

quasi osprey
#

so like

#

everytime i activate a remote event

#

the script cant find the models humanoidrootpart

dark ember
#

ok

woeful ermineBOT
#

studio** You are now Level 1! **studio

dark ember
#

maybe you could make it check for the model's primary part

quasi osprey
#

ive already tryed that