I made a bot that follows the player, but whenever it gets close to the player, it just stutters (as in just freezing up for a bit, it doesn’t change the way it’s facing). Here is a clip demonstrating this:
It’s probably not the code because the code doesn’t take too long to run (I checked, it usually takes about 0.00005
seconds to run, too small to cause twitching). Most of the code is under a MoveToFinished event so it could be that the event itself is slow. There is another thread with a similar issue, but this thread says that that is a bad idea. Should I just use the former thread’s solution or follow the latter thread’s advice? Is there a better solution?
Here’s code just in case:
Bot Code
local Character = script.Parent
local HRP = Character.HumanoidRootPart
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local PathfindingService = game:GetService("PathfindingService")
local Path = PathfindingService:CreatePath()
local waypoints
local currentWaypoint
local Goal
function HomeTo(Destination)
--Generate Path
Goal = Destination
Path:ComputeAsync(HRP.Position, Goal)
--Set variables
local storedGoal = Goal
local waypoints = Path:GetWaypoints()
local currentWaypoint = 1
local waypoint = waypoints[currentWaypoint].Position
--Path recalculation variables
local currentlyCalculating
local pathCalculated
--Reset variables to original values
local function Restart()
storedGoal = Goal
waypoints = Path:GetWaypoints()
currentWaypoint = 1
waypoint = waypoints[currentWaypoint].Position
end
local function RecalculatePath()
coroutine.resume(coroutine.create(function()
currentlyCalculating = true
local startPosition = waypoint
--Check if bot has a future waypoint
if waypoints[currentWaypoint + 1] then
startPosition = waypoints[currentWaypoint + 1].Position
end
--Recalculate path
storedGoal = Goal
Path:ComputeAsync(startPosition, storedGoal)
pathCalculated = true
currentlyCalculating = nil
end))
end
Humanoid:MoveTo(waypoints[currentWaypoint].Position)
RecalculatePath()
local onGoal; onGoal = Humanoid.MoveToFinished:Connect(function()
if pathCalculated then --If a new path was found
pathCalculated = nil
Restart()
Humanoid:MoveTo(waypoint)
else
currentWaypoint = currentWaypoint + 1
--Move agent
if waypoints[currentWaypoint] then
waypoint = waypoints[currentWaypoint].Position
Humanoid:MoveTo(waypoint)
--If agent must jump
if waypoints[currentWaypoint].Action == 1 then
Humanoid.Jump = true
end
else
--[[Will improve this later
while storedGoal == Goal do
wait()
end
RecalculatePath()
Restart()
Humanoid:MoveTo(waypoint)]]
end
end
if storedGoal ~= Goal and not currentlyCalculating then --If goal was changed
RecalculatePath()
end
end)
end
--Yield so I can see the bot move
wait(4)
HomeTo(game.Players:GetPlayers()[1].Character.HumanoidRootPart.Position)
--Change goal every once in a while
while true do
wait(.5)
Goal = game.Players:GetPlayers()[1].Character.HumanoidRootPart.Position
end
Note: The twitching happens even when I don’t move / the destination in the code doesn’t change