Bot doesn't update quick enough to account for slower player speed

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
1 Like

I have tried that, it’s odd because it works to a point, but the bot just stutters faster. I think it just needs to update the position quicker

Character.PrimaryPart:SetNetworkOwner(nil)

Did you set the NetworkOwner of the character’s primary part (HumanoidRootPart) to nil?

1 Like

Yeah, at the top of my script. But even if that was the issue, it would just be lagging sometimes, this issue wouldn’t happen for the most part

You could add a check to see if the player is moving, if it is: break the loop and recall the function itself (to basically recalculate).

Of course you can’t recalculate every single second the player moves so I would just add a debounce to it.

1 Like

Are you thinking like a loops that saves your positions, gets it again after the loops is done and then compares them with magnitude?

Could you also send your entire code?

1 Like

In a dm, i don’t really want to just send it where someone could steal it

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)
1 Like

I think I have tried heartbeat, but I’ll try that again and the other ones too and get back to you

1 Like

Could you show what you tried for the repeat loop?

1 Like
while wait() do
	humanoid:MoveTo(playerRoot.Position)
	if (botRoot.Position - playerRoot.Position).Magnitude > 7  then
		break
	end
end

and a repeat loops that does the same thing

It worked the exact same :confused: so weird

runService.Heartbeat:Connect(function()
	if tracking then
		followPlayer()
	else
		patrol()
	end
end)

Is that just an example? Because I don’t see any raycasting on it.

Oh and, use the task library. (task.wait(), task.spawn(), task.defer)

1 Like

Maybe try using task.spawn(), Not sure if it’ll work.

-- RunService Heartbeat
task.spawn(function()
    -- Code
end)
1 Like

It doesn’t repeat the code, so i don’t know

Yeah, I forgot to add the “canSeePlayer” function in there, just pretend that’s in there

Try using an actual repeat until loop instead of a while loop one.

1 Like

I have, it was the same loop as this one just with repeat instead, same result