Repeat until MouseButton1Up

Hello! Is it possible to use the repeat until loop with an Event? i.e. the loop running until an event is fired.

repeat
	lockPickControl("W")
until Buttons.Up.MouseButton1Up

I already know about the messy “bodge” of having a variable that changes when the button is held down or not, but I have multiple buttons in my project and it would be sort of messy to implement it that way. I’m curious if there is another, cleaner solution.

Thanks in advance!

Yes, you can create a variable at the beginning and have it repeat until the variable is true, then create a connection setting the variable to true when the connection is triggered.

1 Like

https://developer.roblox.com/en-us/api-reference/datatype/RBXScriptSignal

That’s the purpose of the :Wait() instance method, it yields a thread of execution until the event fires a single time. Take the following for example.

local button = script.Parent

button.MouseButton1Click:Wait()

print("Hello world!") --This code isn't executed until the button is clicked.
3 Likes

:wait is better, why do you want to use repeat?

Example code:

local event -- The event

local eventFired = false
while not eventFired do
    print("Not fired!")
    task.wait() -- Example delay, might want to use a different yielding function
end

local connection
event:Connect(function()
    eventFired = true
    connection:Disconnect()
end)

For the last part you can also just use event:Wait() then set eventFired to true (that yields, you can avoid yielding with coroutines, task.spawn, or something else). You’d probably want to put all that code in a function too, something like startLoop() etc.