My bot is having an issue where it won’t update the player’s position quick enough, resulting in stuttering as you can see in the video below
When I’m sprinting, the bot works just fine, but as soon as I stop sprinting, it lags behind a bit
Can anyone help me please? I’ve been trying to fix this all day and I’m stumped
Code:
local function followPlayer()
if playerRoot ~= nil and playerHum.Health > 0 then
local target = playerRoot
local path = getPath(target)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
if path and waypoints and loopWaypoint(waypoints) then
if loopWaypoint(waypoints) ~= nil and loopWaypoint(waypoints).Action == Enum.PathWaypointAction.Walk then
humanoid:MoveTo(loopWaypoint(waypoints).Position)
humanoid.Jump = false
end
if loopWaypoint(waypoints) ~= nil and loopWaypoint(waypoints).Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
end
end
end
end
-------------------------------------------------------
while wait() do
if tracking then
followPlayer()
else
patrol()
end
end
You could use Raycasting to check if the monster can see the player (and is not over a wall), if he’s close enough, do a repeat until with just :MoveTo() and no pathfinding.
You don’t have to worry about it getting stuck because we have a ray that checks if it can see the player or not; if it’s stuck or facing the wall, the repeat loop would just stop and continue pathfinding as usual.
I would put the repeat loop somewhere here:
if path.Status == Enum.PathStatus.Success then
if path and waypoints and loopWaypoint(waypoints) then
--[[PUT REPEAT LOOP HERE]]--
if loopWaypoint(waypoints) ~= nil and loopWaypoint(waypoints).Action == Enum.PathWaypointAction.Walk then
humanoid:MoveTo(loopWaypoint(waypoints).Position)
humanoid.Jump = false
end
if loopWaypoint(waypoints) ~= nil and loopWaypoint(waypoints).Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
end
end
Maybe try switching the while wait() do loop to RunService.Heartbeat and put the functions inside of it? Perhaps, You could also spawn/defer the functions using either task.spawn() or task.defer() — Task spawn will instantly run the code given and don’t yield it, While Task defer will await until the next resumption cycle to run it.
local RunService = game:GetService("RunService")
local Heartbeat = RunService.Heartbeat
Heartbeat:Connect(function()
-- Bot Code
end)