local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Bot = script.Parent
local Humanoid = Bot.Humanoid
local RootPart = Bot.HumanoidRootPart
local RecalculationInterval = math.random(0.5, 1.0) -- Increased interval to reduce twitching
local MinimumRecalculationDistance = 5 -- Recalculate if target moves more than 5 studs
local waypoints
local NextWaypointIndex
local ReachConnection
local BlockConnection
-- Function to find the nearest player
local function findNearestPlayer()
local nearestPlayer = nil
local nearestDistance = math.huge
for _, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local distance = (RootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
if distance < nearestDistance then
nearestDistance = distance
nearestPlayer = player
end
end
end
return nearestPlayer
end
-- Function to check if the bot is stuck on a slope
local function isStuckOnSlope()
local rayOrigin = RootPart.Position
local rayDirection = Vector3.new(0, -1, 0) -- Raycast downward
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Bot}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 10, raycastParams)
if raycastResult then
local normal = raycastResult.Normal
local slopeAngle = math.deg(math.acos(normal.Y)) -- Calculate slope angle
return slopeAngle > 20 -- Jump if the slope is steeper than 20 degrees
end
return false
end
-- Function to check if the bot is facing a wall
local function isFacingWall()
local rayOrigin = Bot.LowerTorso.Position
local rayDirection = Bot.LowerTorso.CFrame.LookVector -- Raycast in the direction the bot is facing
local raycastParams = RaycastParams.new()
-- Ignore the bot and all players
local ignoreList = {Bot}
for _, player in pairs(Players:GetPlayers()) do
if player.Character then
table.insert(ignoreList, player.Character)
end
end
raycastParams.FilterDescendantsInstances = ignoreList
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 5, raycastParams) -- Check 5 studs ahead
if raycastResult then
return true -- Wall detected
end
return false
end
local function FollowPath(targetPosition)
-- Avoid recalculating if the bot is already close to the target
if (RootPart.Position - targetPosition).Magnitude < MinimumRecalculationDistance then
return
end
local path = PathfindingService:CreatePath({
AgentRadius = 2, -- Adjust based on bot size
AgentHeight = 5, -- Adjust based on bot size
AgentCanJump = true,
WaypointSpacing = 4, -- Smaller value for more detailed waypoints
})
local success, errorMessage = pcall(function()
path:ComputeAsync(RootPart.Position, targetPosition)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
NextWaypointIndex = 2
if ReachConnection then ReachConnection:Disconnect() end
if BlockConnection then BlockConnection:Disconnect() end
BlockConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= NextWaypointIndex then
BlockConnection:Disconnect()
FollowPath(targetPosition)
end
end)
ReachConnection = Humanoid.MoveToFinished:Connect(function(reached)
if reached and NextWaypointIndex < #waypoints then
-- Check if the next waypoint requires jumping
if waypoints[NextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
print("Jumping!")
Humanoid.Jump = true
end
NextWaypointIndex += 1
Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
else
ReachConnection:Disconnect()
BlockConnection:Disconnect()
end
end)
Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
else
warn("Pathfinding failed:", errorMessage)
end
end
-- Main loop for staggered recalculation
while wait(RecalculationInterval) do
local nearestPlayer = findNearestPlayer()
if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
FollowPath(nearestPlayer.Character.HumanoidRootPart.Position)
end
-- Force jump if stuck on a slope
if isStuckOnSlope() then
print("Stuck on slope, jumping!")
Humanoid.Jump = true
end
-- Force jump if facing a wall
if isFacingWall() then
print("Facing a wall, jumping!")
Humanoid.Jump = true
end
end
see if this one fixed the problem
I fixed it by making RespectCanCollide = true
1 Like
new issue: ai stutters a lot (charLimit)
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Bot = script.Parent
local Humanoid = Bot.Humanoid
local RootPart = Bot.HumanoidRootPart
local RecalculationInterval = math.random(0.5, 1.0) -- Increased interval to reduce twitching
local MinimumRecalculationDistance = 5 -- Recalculate if target moves more than 5 studs
local waypoints
local NextWaypointIndex
local ReachConnection
local BlockConnection
-- Function to find the nearest player
local function findNearestPlayer()
local nearestPlayer = nil
local nearestDistance = math.huge
for _, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local distance = (RootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude
if distance < nearestDistance then
nearestDistance = distance
nearestPlayer = player
end
end
end
return nearestPlayer
end
-- Function to check if the bot is stuck on a slope
local function isStuckOnSlope()
local rayOrigin = RootPart.Position
local rayDirection = Vector3.new(0, -1, 0) -- Raycast downward
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Bot}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 10, raycastParams)
if raycastResult then
local normal = raycastResult.Normal
local slopeAngle = math.deg(math.acos(normal.Y)) -- Calculate slope angle
return slopeAngle > 20 -- Jump if the slope is steeper than 20 degrees
end
return false
end
-- Function to check if the bot is facing a wall
local function isFacingWall()
local rayOrigin = Bot.LowerTorso.Position
local rayDirection = Bot.LowerTorso.CFrame.LookVector -- Raycast in the direction the bot is facing
local raycastParams = RaycastParams.new()
-- Ignore the bot and all players
local ignoreList = {Bot}
for _, player in pairs(Players:GetPlayers()) do
if player.Character then
table.insert(ignoreList, player.Character)
end
end
raycastParams.FilterDescendantsInstances = ignoreList
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 5, raycastParams) -- Check 5 studs ahead
if raycastResult then
return true -- Wall detected
end
return false
end
local function FollowPath(targetPosition)
-- Avoid recalculating if the bot is already close to the target
if (RootPart.Position - targetPosition).Magnitude < MinimumRecalculationDistance then
return
end
local path = PathfindingService:CreatePath({
AgentRadius = 2, -- Adjust based on bot size
AgentHeight = 5, -- Adjust based on bot size
AgentCanJump = true,
WaypointSpacing = 4, -- Smaller value for more detailed waypoints
})
local success, errorMessage = pcall(function()
path:ComputeAsync(RootPart.Position, targetPosition)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
NextWaypointIndex = 2
if ReachConnection then ReachConnection:Disconnect() end
if BlockConnection then BlockConnection:Disconnect() end
BlockConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= NextWaypointIndex then
BlockConnection:Disconnect()
FollowPath(targetPosition)
end
end)
ReachConnection = Humanoid.MoveToFinished:Connect(function(reached)
if reached and NextWaypointIndex < #waypoints then
-- Check if the next waypoint requires jumping
if waypoints[NextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
print("Jumping!")
Humanoid.Jump = true
end
-- Move to the next waypoint
NextWaypointIndex += 1
Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
else
ReachConnection:Disconnect()
BlockConnection:Disconnect()
end
end)
Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
else
warn("Pathfinding failed:", errorMessage)
end
end
-- Main loop for staggered recalculation
local lastTargetPosition = nil
while wait(RecalculationInterval) do
local nearestPlayer = findNearestPlayer()
if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
local targetPosition = nearestPlayer.Character.HumanoidRootPart.Position
-- Only recalculate if the target has moved significantly
if not lastTargetPosition or (targetPosition - lastTargetPosition).Magnitude > MinimumRecalculationDistance then
FollowPath(targetPosition)
lastTargetPosition = targetPosition
end
end
-- Force jump if stuck on a slope
if isStuckOnSlope() then
print("Stuck on slope, jumping!")
Humanoid.Jump = true
end
-- Force jump if facing a wall
if isFacingWall() then
print("Facing a wall, jumping!")
Humanoid.Jump = true
end
end
for me, this code didnt stutter with the test map you made
yo do you have updates on the ai, is it working fine?
So sorry, my WiFi cut out from pc, I’ll try it in few hours
You tried fixing the lag? (charLimit)
it’s also like, supperr glitchy.
its better if i see a video or if i can have the model with the script so i can see whats wrong
it’s the same as yours. Didn’t modified anything.
i tested mine and like its working fine for me