Robot Companion Path Lagging

So I want to make the Robot stop lagging kind of or at least try to(I’m also new to Scripting by the way)

So this is the script but please ignore the other stuff unless if it’s the problem:robloxapp-20210802-0024101.wmv (2.8 MB)

local ReplicatedStorage = game:GetService(“ReplicatedStorage”).Robot
local RunService = game:GetService(“RunService”)

RunService.Stepped:Connect(function() – Runs this script forever

local KadenogiPlayer = game.Players:FindFirstChild("Kadenogi")
local Kadenogi = game.Workspace:FindFirstChild("Kadenogi")
local Robot = game.Workspace:FindFirstChild("Robot")

local MaxDistanceT = 200 -- T means Teleport
local MaxDistanceTF = 50 -- TF means Too Far
local MaxDistanceF = 20 -- F means Far
local MaxDistanceC = 10 -- C means Closest

-- Checks if Kadenogi(Which is my User) is in the game. If it is, then the Robot will be moved into Workspace
if KadenogiPlayer then
	wait(1)
	ReplicatedStorage.Parent = game.Workspace
end

-- Checks if Kadenogi(Which is my User) is in the game. If it is not, then the Robot will be moved back into ReplicatedStorage
if KadenogiPlayer == nil then
	wait(1)
	ReplicatedStorage.Parent = game.ReplicatedStorage
end

-- Makes the Robot follow only Kadenogi(Which is my User)
if Kadenogi and Robot then
	local PathParam = {
		["AgentRadius"] = 3,
		["AgentHeight"] = 5,
		["AgentCanJump"] = true
	}

	local Destination = Kadenogi.HumanoidRootPart.Position + Vector3.new(-3.5,0,4.5)
	local RobotWRP = Robot.HumanoidRootPart.Position

	if Kadenogi and Robot then
		local function WalkTo(Destination)
			local Path = game:GetService("PathfindingService"):CreatePath(PathParam)

			Path:ComputeAsync(RobotWRP, Destination)

			if Path.Status == Enum.PathStatus.Success then
				for index, Waypoint in pairs(Path:GetWaypoints()) do
					Robot.Humanoid:MoveTo(Waypoint.Position)
					Robot.Humanoid.MoveToFinished:Wait()
				end
			end
		end
		WalkTo(Destination)
	end
end

-- Makes Robot change Speed if Robot is too far away, and returns back to normal Speed if the Robot is close enough
if Kadenogi and Robot then
	if (Kadenogi.HumanoidRootPart.Position - Robot.HumanoidRootPart.Position).magnitude >= MaxDistanceTF then
		Robot.Humanoid.WalkSpeed = 50
	end
	if (Kadenogi.HumanoidRootPart.Position - Robot.HumanoidRootPart.Position).magnitude >= MaxDistanceF then
		Robot.Humanoid.WalkSpeed = 23
	end
	if (Kadenogi.HumanoidRootPart.Position - Robot.HumanoidRootPart.Position).magnitude <= MaxDistanceC then
		Robot.Humanoid.WalkSpeed = 19
	end
end

-- Makes Robot Teleport if Kadenogi(Which is my User) and the Robot way too far away
if Kadenogi and Robot then
	if (Kadenogi.HumanoidRootPart.Position - Robot.HumanoidRootPart.Position).magnitude >= MaxDistanceT then
		Robot.HumanoidRootPart.CFrame = Kadenogi.HumanoidRootPart.CFrame + Vector3.new(-3.5,0,4.5)
	end
end

end)

Welcome Kaden!

I can see you’ve put a lot of thought into this code. It would be my pleasure to bring some issues to your attention that stick out to me.

The teleport / speed statements should be combined, and should be in an if-elseif-else format. This is because they are mutually exclusive (only one can happen) and the else case should exist otherwise it wont always be set and depends on the last case hit (i.e. if the distance is in the range (MaxDistC, MaxDistF) then the Walkspeed can be either 23 or 19).

The main issue that I see is that the path is being recomputed each step, and following the previously computed path is not being canceled. Instead, there should be one path object, and ComputeAsync called every step (or however often you decide you can spare resources for).

You should then disconnect the signal for the MoveToFinished event on the old waypoints and reconnect it for the new waypoints. You may need to check that the first new waypoint isn’t immediately infront of the robot as well.

To disconnect the signal, use the event :Connect() method which returns a connection object you can call :Disconnect() on as shown here: RBXScriptSignal | Roblox Creator Documentation . This means making a new variable to hold the connection between steps.

You may also need to ensure the robot’s network owner is the server. If not, MoveToFinished has some latency. BasePart | Roblox Creator Documentation

I’ll be writing a controller that handles all of this for you as part of my pathfinding project. If you would like to follow the progress, you can join the discord server here: Polaris-Nav

I think I understand. A question I have is do I do this for the NetworkOwner?

local KadenogiPlayer = game.Players:FindFirstChild(“Kadenogi”)

local Robot = game.Workspace:FindFirstChild(“Robot”)

if KadenogiPlayer and Robot then
KadenogiPlayer:SetNetworkOwner(Robot)
end

Would this be correct?