Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I reduce client lag when firing a remote event multiple times?

I have developed a BETA version of a game, think of papers please, it's a loop of a random frame showing, detecting to keypresses, sending it off to the server, and moving a part, that loop repeats with a while true do loop.

Whenever the frame shows, the user can either press [F] or [G], and dependant on the information, it's either right or wrong. The problem is that I need to wait for the user to make the input so I used a remote event to tell the server when that input occurred, but it causes more lag each time it is fired, by around 3 loops in a lag spike is noticeable.

I thought of making the part movement client-side (because it is a single-player game) but I have no idea how to do that, and if it ever becomes a multiple-player game, that would not really be practical.

How can I repeat the loop indefinitely without lag spikes occurring?

This is the loop:

while true do
	tween_car_in()
	
	spawn_docs()
	
	user_input()
	
	choose_passport()
	
	check_option()
	
	destroy_docs()
	
	tween_car_out()
	
	reset_cycle()
end

This is the relevant function:

repeat
	rs.OptionPicked.OnServerEvent:Connect(function()
		chosen = true
		rs.OptionPicked:FireClient(player)
	end)
	wait(0.25)
until chosen == true

And this is the keypress detection:

user_input.InputBegan:Connect(function(key_input)
	if key_input.KeyCode == Enum.KeyCode.F and picked == false and script.Parent.Visible == true then
		--Irrelevant Code
		wait(2)
		game.ReplicatedStorage.OptionPicked:FireServer(true)
		--Irrelevant Code
	elseif key_input.KeyCode == Enum.KeyCode.G and picked == false and script.Parent.Visible == true then
		--Irrelevant Code
		wait(2)
		game.ReplicatedStorage.OptionPicked:FireServer(true)
		--Irrelevant Code
	end
end)

3 comments

0

Note that in the relevant function I also need to pass the same event from client to client so I need to pass it through the server and then pass it onto the other client

0

Try putting the loop in the script handling the remote event.

0

That is the script handling the loop, it's only purpose is to run the loop. All of the other actions (such as making the frame appear) are done through local scripts. All this script does is tween a few parts, play some animations, pick a random number (so it can display the relevant frame), and just pass this information through remote events to the local scripts so they can do their part.

1 answer

First of all, never use While Trues, they are not healthy for any kind of behavior, instead of it use While wait() do. Either way there are two ways you can do this, one if having the while on the server side meaning u send all the variables that can only be obtained on the client side to the server side and do the action there, the second option is trying to make this event based instead of using a while, per example if u only want something to happen after the user presses a button you would use a key pressed event and so on

2 comments

0

That's objectively untrue. while true do is the preferable of writing the loop, whereas while wait() do is not. Also, your response would be better placed as a comment, not an answer, since you didn't completely answer his question.

0

The while true do is already on the server side, and making it event based would not be possible because there is not (or at least I don't think there is) a equivalent of goto [line] as in Javascript which is the only way I think I could make it not a loop, it would be great if you would put in some code samples showing me how to not make it a loop, thanks.

This discussion is preserved as it was. New answers, comments, and votes are closed.