Obby Movement Depending On Client Internet Connection

Hello, I am making an obby and I had my friend test it out for me. He has a pretty bad internet connection and he told me that the movement of parts in my game are relying on his internet connection, basically that they lag with him. He said that I should make it like Toh, which doesn’t have this problem. To move the parts in my obby, I am using a script, and I was wondering if I change the script to localscript, it would stop it from depending on the client’s internet connection. What do you guys think is the solution to this problem?
Thanks :smiley:

1 Like

If you posted your code, it would help us fix your code :slight_smile:

Hello, this is one of the scripts used to rotate a part
local part = script.Parent

local function rotate()
	for degree=0, 361, 0.8 do
		
		part.Orientation = Vector3.new(part.Orientation.X, part.Orientation.Y, degree)
		wait()
	end
end



while true do
	rotate()
	wait()
end

The problem isn’t your friend’s internet connection, but rather his frame rate. I think it would be best to make the rotation of the parts relative to the player’s frame rate. To do this we can use the RunService.RenderStepped event, which fires every frame prior to the frame being rendered.

Here’s how something like this would work:

(This is client sided)

local RunService = game:GetService("RunService");

local function rotate()
	for degree = 0, 361, 0.8 do
		-- People generally use CFrame.Angles to change orientation
		local Angle = CFrame.Angles(0, 0, degree);
		part.CFrame = part.CFrame * Angle;

		RunService.RenderStepped:Wait();
	end;
end;

while true do
	rotate();
end;

Sources:

Do the same for the “while true do” below, that runs on every frame and is very bad

The RenderStepped event has been encouraged to yield loops by a lot of refutable developers. If you’re trying to avoid while true do, then you could try something like this:

local RunService = game:GetService("RunService");

local bind = string.format("%sRotate", part.Name);
RunService:BindToRenderStep(bind, Enum.RenderPriority.Last.Value, function()
	local Angle = CFrame.Angles(0, 0, math.pi / 100);
	part.CFrame = part.CFrame * Angle();
end);

Hello, I will try that solution, but I don’t understand what you mean by “this is client sided”. Does that mean that it’s a localscript?

Thanks :smiley:

Yes it does! LocalScripts run on the client side :slightly_smiling_face:

Alright thanks, I will try it out!

By the way, sometimes I rotate a part using TweenService, is TweenService also affected by the FPS?

Think of your Roblox game as a video. Videos are comprised of a series of images (frames). If the frame rate of a video is low, then the video will be choppy. It’s the same way on Roblox. Every frame is a new image. If there is latency between one frame and the next while the tween is in progress, then it’ll look like the tween has skipped a few studs.

The reason I decided to use the RenderStepped event is because it only fires every frame prior to the frame being rendered. This means that if your frame rate drops, the loop that rotates the part will stop, when the next frame is rendered it will pick back up from where it started.

So the short answer to your question is; kind of. The tween will continue playing even if your frame rate has dropped. So the next time you view it, it will be most likely a few frames farther than when your screen froze.

This is probably not the best way to answer your question, if you need more information, I encourage you to read this.

Ok, I get it. But will it be the same if its a script instead of localscript?

To my understanding, no matter which type of script it’s in, the same thing will happen. However I haven’t seen the code that goes behind the Tween:Play() method, so I can’t say for sure.

I should have specified, but I was talking about the script that you showed me.

It will not be the same in a script. The RenderStepped event can only be connected to on the client. It also will only fire prior to the next frame being rendered, on the client. Putting it in a normal script would give you an error.

I see, I haven’t worked with localscripts much. So I don’t know where I should put the localscript and how to connect it. Should I use remoteEvents?