[Help me pls!] How to activate a loop and deactivate it

Hello! I have a question, is there a way to activate and deactivate a loop, I know how to make a loop with while true do but there are times where I want to do repetitive effects, for example when holding the letter “C” You charge energy with repetitive effects and when it ends the repetitive effect stops

while loops are more than just while true do, although this is seems to be the most common use for whatever reason. Generally while true do loops something that should rarely be used because there are almost always better options.

In your case, you can do something like:

userInputService.InputBegan:Connect(function(key, gameProcessed)
    if gameProcessed then
        return
    end
    while userInputService:IsKeyDown(Enum.KeyCode.C) do
        -- ensure you have a task.wait or some yielding code, otherwise it's going to cause major performance issues
    end
end)

Another neat thing about while loops is that you don’t really have to use an if statement checking if the key is down since if it isn’t down, the loop will never run.

1 Like

Just use a function and check if the UserInputState is beginning or ending!