#My NPC AI isn't working

97 messages · Page 1 of 1 (latest)

ionic mulch
#

My ai is coded properly with agents and all but he still gets stuck on walls sometimes

#

local teddy = script.Parent
local humanoid = teddy.Humanoid
teddy.PrimaryPart:SetNetworkOwner(nil)

local wait_time = 1

local function canSeeTarget(target)
local origin = teddy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)

local hit, pos = workspace:FindPartOnRay(ray, teddy)


if hit then
    if hit:IsDescendantOf(target) then
        return true
    end
else
    return false
end

end

local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget

for index, player in pairs(players) do
    if player.Character then
        local target = player.Character
        local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
        
        if distance < maxDistance and canSeeTarget(target) then
            nearestTarget = target
            maxDistance = distance
        end
    end
end

return nearestTarget

end

local function getPath(destination)
local PathfindingService = game:GetService("PathfindingService")

local pathParams = {
    ["AgentHeight"] = 7.037,
    ["AgentRadius"] = 3,
    ["AgentCanJump"] = false
}

local path = PathfindingService:CreatePath(pathParams)

path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)

return path

end

local function runanim()
local RunAnim = humanoid:LoadAnimation(script.Parent.Run)
local run = 0
if run == 0 then
RunAnim:Play()
run = 1
end
RunAnim:Stop()
end

#

local function attack(target)
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

if distance > 6.5 then
    wait_time = 0.25
    
    humanoid:MoveTo(target.HumanoidRootPart.Position)
else
    
    wait_time = 1
    local attackAnim = humanoid:LoadAnimation(script.Attack)
    attackAnim:Play()
    target.Humanoid.Health = 0
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)
            break
        else
            print("Moving to ", waypoint.Position)
            humanoid:MoveTo(waypoint.Position)
            humanoid.MoveToFinished:Wait()
        end
    end
else
    humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
end

end

function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
local chosenwaypoint = waypoints[randomNum]
walkTo(chosenwaypoint)
--for i, v in pairs(workspace.waypoints:GetChildren()) do
--local randomNum = math.random(1, #v.Parent)
--walkTo(v)
--end
end

while wait(wait_time) do
patrol()
end

#

this is the whole script

hard hull
#
local function attack(target)
    local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
    
    
    if distance > 6.5 then
        wait_time = 0.25
        
        humanoid:MoveTo(target.HumanoidRootPart.Position)
    else
        
        wait_time = 1
        local attackAnim = humanoid:LoadAnimation(script.Attack)
        attackAnim:Play()
        target.Humanoid.Health = 0
    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)
                break
            else
                print("Moving to ", waypoint.Position)
                humanoid:MoveTo(waypoint.Position)
                humanoid.MoveToFinished:Wait()
            end
        end
    else
        humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
    end
end

function patrol()
    local waypoints = workspace.waypoints:GetChildren()
    local randomNum = math.random(1, #waypoints)
    local chosenwaypoint = waypoints[randomNum]
    walkTo(chosenwaypoint)
    --for i, v in pairs(workspace.waypoints:GetChildren()) do
        --local randomNum = math.random(1, #v.Parent)
        --walkTo(v)
    --end
end

while wait(wait_time) do
    patrol()
end
#

Just for the code formatting ig

ionic mulch
#

that's not the full script btw

hard hull
#

Oh

ionic mulch
#

yh sorry

hard hull
#

You have a video of him like failing

ionic mulch
#

yh

#

but sometimes it gets stuck infinitely

#

sorry for the long vid

#

u only need like 10 secs

hard hull
#

Lol

#

Funni

#

Anyway

ionic mulch
#

yh lol

vocal wingBOT
#

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

hard hull
#

Did you pass in any path params

#

When creating the path

ionic mulch
#

local pathParams = {
["AgentHeight"] = 7.037,
["AgentRadius"] = 3,
["AgentCanJump"] = false
}

vale lodge
#

bros

#

not

#

npcing

hard hull
#

He's exited the matrix

#

Ig adjust agents radius

#

It seems like he might be catching the wall there

#

Increase it until it works ig

ionic mulch
#

alr

hard hull
#

Probably won't work but worth a try

#

The first idea that comes to your head and seems reasonable is always the worst idea

#

-Some Yt guy

vale lodge
#

its

#

normal

#

get

#

stuck

#

wall

#

its cuz pathfind

#

stoopid

hard hull
#

As a pathfind algorithm I can confirm that I am stupid

ionic mulch
#

alr

#

for some reason it always gets stuck on the same wall

hard hull
#

Hmmmmmm🤔

ionic mulch
#

ikr

hard hull
#

Anything unique abt that wall?

ionic mulch
#

no

#

but it seems like it's not doing it anymore

#

idk why lmao

hard hull
#

Just go with it ig

#

If it doesn't get there in a certain amt of time maybe you can't recomputate the path

ionic mulch
#

yh

hard hull
#

Anyway the amt of times this has happened in piggy is like astoundingly large

#

So I would say it might be unavoidable

ionic mulch
#

yh

#

well thank u vm

hard hull
#

There is this tho

#

My friend told me abt this module

ionic mulch
#

I'll try it later

hard hull
#

Haven't tried it myself yet but it's better than the shit rblx has

ionic mulch
#

if i see it doesnt work anymore

hard hull
#

Ok

ionic mulch
#

alr

hard hull
#

Seeya

ionic mulch
#

gl

vocal wingBOT
#

studio** You are now Level 7! **studio

ionic mulch
#

cya

#

@hard hull actually 1 more thing

vocal wingBOT
#

studio** You are now Level 2! **studio

hard hull
#

?

ionic mulch
#

im trynna make the npc do a run animation when it chases u

#

how would i do it

hard hull
#

Well you know how to play the animation right?

ionic mulch
#

yh

hard hull
#

Uh

#

Lemme see

ionic mulch
#

well i can play it but then it doesnt stop

#

so ig i wanna see how u would do it

hard hull
#

So uh make the animation looped and

#

Nvm idk

#

Give me a sec

ionic mulch
#

alr

hard hull
#

So like after the MoveTo statement I would play the run animation where its looped

#

Then I would wait for the MoveTo to be done

#

Then I would stop the run animation

#

Idk that's my logic

ionic mulch
#

alr

#

it seems like it keeps replaying the animation

hard hull
#

Ig you could also make looped false afterwards for good measure

ionic mulch
#

no sorry i meant like it replays the first second of the animation meaning that it keeps playing the animation instead of letting it play out

hard hull
#

Idk

ionic mulch
#

alr

#

dw

buoyant anvil
#

Yo

#

I used this tutorial for my game it’s okay but I changed a lot of things. First off, you can’t really fix it