Alright so I’m currently in the process of making an AI that will move to a random part in a dedicated folder. The issue is it currently teleports to the starting place like normal but then stays there while occasionally playing the walking animation. Its my first time messing with pathfinding so I might just be missing something but Idk.
Heres the code for the function that handles teleporting the AI to the random starting place
-- Active logic
local function activeTeleport()
local targetPart = getRandomActivePart()
if targetPart then
BigFella:PivotTo(targetPart.CFrame)
end
ModeValue.Value = MODES.PassiveWander -- Changes the current state from Active to PW after it teleports
end
Here’s the snippet that handles the movement.
local function passiveWander()
local targetPart = getRandomPassivePart()
if not targetPart then
playIdle()
task.wait()
return
end
local path = PathfindingService:CreatePath({
AgentRadius = 8, -- Actual Radius is 18
AgentHeight = 24,
AgentCanJump = true, -- Might need to disable that later
AgentJumpHeight = 10,
AgentMaxSlope = 45
})
path:ComputeAsync(HumanoidRootPart.Position, targetPart.Position)
if path.Status ~= Enum.PathStatus.Success then
playIdle()
task.wait()
return
end
local waypoints = path:GetWaypoints()
playWalk()
for _, waypoint in ipairs(waypoints) do
if ModeValue.Value ~= MODES.PassiveWander then break end -- Makes sure the current state is set to PW
Humanoid:MoveTo(waypoint.Position)
local reached = false
local conn
conn = Humanoid.MoveToFinished:Connect(function()
reached = true
if conn then conn:Disconnect() end
end)
local timer = 0
while not reached and ModeValue.Value == MODES.PassiveWander do
-- Check for nearby players to switch current state to Aggro
for _, player in pairs(Players:GetPlayers()) do
local char = player.Character
if char and char:FindFirstChild("HumanoidRootPart") then
if (char.HumanoidRootPart.Position - HumanoidRootPart.Position).Magnitude <= AGGRO_DISTANCE then
ModeValue.Value = MODES.Aggro
stopAllAnimations()
return
end
end
end
task.wait(0.2)
timer = timer + 0.2
if timer > 10 then break end
end
end
playIdle()
task.wait(math.random(.1,.5)) -- SHOULD let it wait before moving on :/
end
and if it helps here is the random part function
local function getRandomPassivePart()
local parts = {}
for _, obj in Workspace.LamePaths:GetChildren() do
if obj:IsA("BasePart") then
table.insert(parts, obj)
end
end
if #parts > 0 then
return parts[math.random(1, #parts)]
end
return nil
end
I was originally using
Humanoid:MoveTo(targetPart.Position)
instead of pathfinding services but I was tired of the AI getting stuck on things.