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

How do I loop something until an event fires?

How can I keep repeating "plrpos = plrpos + Vector2.new(0.05, 0)" while the W key is down and then stop when the W key is up? I have this code, it works but how can I make it stop when W is up.

game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.W then
			if input.UserInputState == Enum.UserInputState.Begin then
				repeat
					wait()
					plrpos = plrpos + Vector2.new(0.05, 0)
				until ???--What can I do here to loop this until the player stops pressing the W key
			end
		end
	end	
end)

1 answer

✓ Accepted answer

I looked on the Roblox Wiki and I found the solution. I've tested this and it works.

game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.W then
            if input.UserInputState == Enum.UserInputState.Begin then
                repeat
                    wait()
                    plrpos = plrpos + Vector2.new(0.05, 0)
                until game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.W) == false
            end
        end
    end 
end)

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