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