What do you want to achieve?
I want to make a moving character.
What is the issue?
My script works fine (I think so because when I tested it on a dummy it worked), but I might have some things to change to fix the issue. Basically, when the character is following me it slides back and forth.
What solutions have you tried so far?
Didn’t have any possible solutions. The issue is NOT with the pairs loop.
This is how it looks like:
robloxapp-20220106-1257531.wmv (3.9 MB)
while true do
for _, enemy in pairs(attackEnemies) do
local specificEnemyData = enemyData[enemy.enemyId.Value]
local closestCharacter = nil
local distance = specificEnemyData.aggroRange
local range = enemy.range
for _, player in pairs(players:GetChildren()) do
local character = player.Character
if character then
local humanoidRootPart = character.HumanoidRootPart
local magnitude = (range.Position - humanoidRootPart.Position).magnitude
if magnitude < distance then
closestCharacter = character
distance = magnitude
end
end
end
if closestCharacter then
if distance < specificEnemyData.notFollowDistance then
else
local path = pathfindingService:CreatePath(specificEnemyData.pathParameters) --{AgentRadius = 40,AgentHeight = 70,AgentCanJump = false,WaypointSpacing = 3,Costs = {deathZones = math.huge,possibleZones = 10} }
local success, errorMessage = pcall(function()
path:ComputeAsync(enemy.range.Position, closestCharacter.HumanoidRootPart.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
local waypoints
local currentWaypoint = 0
waypoints = path:GetWaypoints()
local blockedConnection = path.Blocked:Connect(function(blockedWaypoint)
if blockedWaypoint >= currentWaypoint + 1 then
path:ComputeAsync(enemy.range.Position, closestCharacter.HumanoidRootPart.Position)
waypoints = path:GetWaypoints()
end
end)
while true do
currentWaypoint = currentWaypoint + 1
print(currentWaypoint)
if waypoints[currentWaypoint] then
enemy.bossModel.Humanoid:MoveTo(waypoints[currentWaypoint].Position)
else
blockedConnection:Disconnect()
break
end
enemy.bossModel.Humanoid.MoveToFinished:Wait()
end
else
print(errorMessage)
return errorMessage
end
end
end
end
wait(1)
end