How do I make repeat until not key pressed?

I want to make my own custom game not using the official Roblox movement I have made my own camera turn but I want to make the camera move. Pressing “W” would make you move in the direction you are facing in.

I searched the dev forum but nothing was what I wanted…

here is the game if you want to see what I tried: My custom roblox game - Roblox

this is my camera turn and move script:

local Mouse = game.Players.LocalPlayer:GetMouse()

local UserInputService = game:GetService("UserInputService")

local MouseX = 0
local MouseY = 0

local CamPart = game.Workspace.Player.CameraPart

while wait() do
	
	game.Workspace.Camera.CameraType = Enum.CameraType.Scriptable
	
	game.Workspace.Camera.CFrame = game.Workspace.Player.CameraPart.CFrame
	
	MouseX = Mouse.X * -0.7
	MouseY = Mouse.Y * -0.35 + 55
	
	game.Workspace.Player.CameraPart.Orientation = Vector3.new(MouseY,MouseX,0)
	
	UserInputService.InputBegan:Connect(function(Input)
		
		if Input.KeyCode == Enum.KeyCode.W then
			
			repeat wait()
				
				local lv = game.Workspace.Player.CameraPart.CFrame.LookVector
			
				local cf = game.Workspace.Player.CameraPart.CFrame
				
				game.Workspace.Player.CameraPart.CFrame = cf + lv * Vector3.new(0.05,0.05,0.05)
				
			until UserInputService.InputEnded == Enum.KeyCode.W
							
		end
		
	end)
	
end

Hey there!

Don’t use a repeat loop to wait for a key to no longer be pressed, use the UserInputService.InputEnded event.
You can check to see which key was pressed the same way as InputBegan.

1 Like

maybe something like this:

local userInputService = game:GetService("UserInputService")
local pressed = false

userInputService.InputBegan:Connect(function(input, isTyping)
    if not isTyping then
        if input.KeyCode == Enum.KeyCode.W then
            if not pressed then
                pressed = true
                repeat wait()
                    -- your code
                until not pressed
            end
        end
    end
end)

userInputService.InputEnded:Connect(function(input, isTyping)
    if not isTyping then
        if input.KeyCode == Enum.KeyCode.W then
            pressed = false
        end
    end
end)
3 Likes

Omg I actually forgot about variables…

thx that worked!

1 Like

No problem! Glad I could help :slight_smile:

2 Likes