Help with RenderStepped

I’ve been using RunService.RenderStepped to make a player’s pets follow them in a project I’m working on, and the method I’ve been using to stop them from following the player is rather than having a hardcoded RenderStepped like that, I use RunService:BindToRenderStep with the function that makes the pets follow the player, and then unbinding it when I want them to stop moving, and then once it’s unbinded, I use Humanoid:MoveTo on the pet to move it to where it needs to go, which is an invisible brick following the player’s cursor…

The problem with this, despite it working, is that for some strange reason it lags the entire game when the pet starts walking over. Normally I’d be able to deal with something like this myself, but I haven’t scripted in months. Keep in mind that I’ll be optimizing this code a lot more when it’s actually finished, currently it’s very very messy.

-- Wait for player to click and move NPC to their mouse's position
Mouse.Button1Down:Connect(function()
				
				if SelectedUnit == Unit1 then
					LockUnit1 = false
				elseif SelectedUnit == Unit2 then
					LockUnit2 = false
				elseif SelectedUnit == Unit3 then
					LockUnit3 = false
				end
				
				MoveUnit()
				RunService:UnbindFromRenderStep("tempUpdate")
				DeactivateSelection()
				MousePointerBrick:Destroy()
				DistanceVisualizer:Destroy()
			end)
-- Move NPC
local function MoveUnit()
		if SelectedUnit == Unit1 then
			LockUnit1 = false
		elseif SelectedUnit == Unit2 then
			LockUnit2 = false
		elseif SelectedUnit == Unit3 then
			LockUnit3 = false
		end

		SelectedUnit:WaitForChild("Humanoid"):MoveTo(MousePointerBrick.Position)
		SelectedUnit.Highlighter.Transparency = 1
	end

Please note that I already know I have to move the NPC in a ServerScript, it doesn’t matter yet.

It’s probably lagging the whole game, because you’re running a lot of code. I would simplify the code greatly, it’s really not that hard to make. Or you could use a simple task.wait() while loop and lerp the pets towards their positions every frame or 2. That would be more simple. Also, if you’re going to have a lot of pets, make sure they’re not colllidable, castshadow, cantouch and canquery is off on them (preferentially) because this also causes lag.

How would I do that and add the possibility to toggle it on and off? Each player has three pets, there’s a limit of six players per game. The point of the game is that the pets follow you, and you can click them and then click an enemy and they walk over and attack it.

Well, in that aforementioned loop, you can use a BoolValue, or a boolean and if it’s true, then you can continue to update and lerp the pets positions. But if it’s false, just use continue.

I see. I tried similar with the RenderStepped method, having a BoolValue that I could toggle on and off, and it just completely ignored it and kept moving the pets. Hopefully this will work better, I’ll let you know.

Well, if you’re in RenderStepped, you would need to use return instead, not continue.

Judging from what you described, there is no need to use RenderStepped at all. I am not sure if you doing it, but there is absolutely no need to use Humanoid:MoveTo() each frame.

You only want to use RenderStepped, when you NEED to recalculate something each frame. Good example would be pet following mouse cursor. It seems the amount of calculations in binded function, exceeds allocated execution time - hence the game lags.

In your case however, all you need to do is to cast a screen-to-workspace ray, when player clicks a mouse. Then instead of using invisible block, move pet to that ray location.

EDIT:
For pets to follow player, you also do not need to use RenderStepped. That is an overkill.
For example, you’d want pets to update direction every second or so, to reduce the load on the server.

1 Like

Does the entire game start lagging or just the pet trying to reach its destination?

It lags the player’s entire game, but it doesn’t lag for other players, it’s like the camera drops to 15fps and the pet stutters across the screen.