What can I use instead of repeat until?


I’ve written this code for a training where you press random generated buttons in-order to do the training but for some reason the remote event if fired more than once when I use this code.

Multiple things here

First, don’t use repeat/while loops for verifying values, this is known as polling and causes unnecessary stress to your code. Instead, use proper conditioning for debounces and if needed Bindable Events

Second, you don’t need to use if elseif statements when trying to get a random value. Instead, you should put all the Keys into a table and just pick one from the array

Third, don’t repeatedly clone objects if you’re going to use them again, and also always parent instances after you’ve set their properties due to performance

Fourth, mouse.KeyDown is a legacy method and is not intended for new code. Instead, you should use UserInputService which allows for better fine tuning and makes life easier

And finally, the sole reason why it’s firing the event multiple times is because you are connecting the KeyDown Signal each time you iterate which can also cause a memory leak. Always connect RBXScriptSignals outside of iterations unless it pertains to the iterated value

3 Likes