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

How to Repeat x While a Player is Holding Down a Key?

Hey everyone!

First off, Happy Holidays!

Second, I was trying to make a system where when a player holds down "W", their camera slowly increases. I was hoping to get a kind of loop style where if the player holds down W, then it repeats a code telling the camera to move up by 0.01 studs. I honestly had no clue how to do this. Here is my script right now. It's a bit of a mess.

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(inputObject,gameProcessedEvent)
		if inputObject.KeyCode == Enum.KeyCode.W then
			print"e"
			local cam = build.TowerView.CameraPosition
			if cam.Position.Y <= build.TowerView.Max.Position.Y then
				cam.CFrame = CFrame.new(cam.Position.X,cam.Position.Y+0.01,cam.Position.Z) * CFrame.Angles(math.rad(rot.X),math.rad(rot.Y),math.rad(rot.Z))
				game:GetService("Workspace").CurrentCamera.CFrame = CFrame.new(cam.Position)* CFrame.Angles(math.rad(rot.X),math.rad(rot.Y),math.rad(rot.Z))
				wait()
			end
			wait()
		end
	end)

If you could also suggest ways to clean it up, that would be great!

Thanks!

1 comment

0

Also I do know that holding down a key will only perform the action once, I would just like to know how I could make it run continuously while the player is holding down a key.

2 answers

✓ Accepted answer

The reason this doesn't work is because UIS.InputBegan only fire's when an input began so it will only move the camera once, so what you want to do is something like this:

local UIS = game:GetService("UserInputService")

local keyWDown = false--This will be for our loop

UIS.InputBegan:Connect(function(inputObject,gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.W then
		keyWDown = true
        local cam = build.TowerView.CameraPosition
		repeat --Repeats this code until keyWDown is equal to false
			if keyWDown == false then--This is just extra protection
				break
			end
	        if cam.Position.Y <= build.TowerView.Max.Position.Y then
	            cam.CFrame = CFrame.new(cam.Position.X,cam.Position.Y+0.01,cam.Position.Z) * CFrame.Angles(math.rad(rot.X),math.rad(rot.Y),math.rad(rot.Z))
	            game:GetService("Workspace").CurrentCamera.CFrame = CFrame.new(cam.Position)* CFrame.Angles(math.rad(rot.X),math.rad(rot.Y),math.rad(rot.Z))
	            wait()
	        end
		until keyWDown == false
    end
end)

UIS.InputEnded:Connect(function(inputObject,gameProcessedEvent)--This is when an input ends
	if inputObject.KeyCode == Enum.KeyCode.W then
		keyWDown = false--This ends the repeating loop
	end
end)

2 comments

0

Thanks!

0

No problem!

In a case like this their is 2 ways in which I would do it. I would use the RunService.RenderStepped event for the update every frame and create a table which logs the condition of current inputs. Then every frame check if that key is pressed down and if it is then move the camera.

The other option would be to use a coroutine to create a new thread with a loop in it that loops until the InputObject.UserInputState is equal to Enum.UserInputState.End.

Example of what I would do:

local Keys = {}

game:GetService("UserInputService").InputBegan:Connect(function(Input, gameprocessed)
	if not gameprocessed then -- checks that the input isn't interacting with anything from the game core
		Keys[Input.KeyCode] = true
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(Input)
	Keys[Input.KeyCode] = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if Keys[Enum.KeyCode.W] then
		-- move camera
	end
end)

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