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

How do you make a keyboard input "automatic"?

I'm trying to make a rail shooter game (like star fox) but I ran into one problem. The player has to "mash" one of the moving keys, as opposed to how I wanted it where they can just hold the key down.

code inside of a local script, which has been placed in the starter pack

local BodyPositionPosX = 8
local BodyPositionPosY = 1
function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.A then
		BodyPositionPosX = math.min((BodyPositionPosX + 2), 24)
		game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
		
	elseif inputObject.KeyCode == Enum.KeyCode.D then
		BodyPositionPosX = math.max((BodyPositionPosX - 2), -16)
		game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
		
	elseif	inputObject.KeyCode == Enum.KeyCode.W then
		BodyPositionPosY = math.min((BodyPositionPosY + 2), 24)
		game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
		
	elseif inputObject.KeyCode == Enum.KeyCode.S then
		BodyPositionPosY = math.max((BodyPositionPosY - 2), 0)
		game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
		
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

2 comments

0

Use input ended event

0

I tried switching it and it only activated when you let go of the button not automatic

1 answer

✓ Accepted answer

Guess what, I DID IT!

You need to use the InputEnded to see if the input that was ended is the current key so, we create a variable runrepeat and a variable currentkeycode Then, when a key is pressed, we use a repeat loop, and wait for the key to get InputEnded!

Here's the code (I tested it!):

local BodyPositionPosX = 8
local BodyPositionPosY = 1
local runrepeat = false
local currentkeycode
function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.A then
		currentkeycode = Enum.KeyCode.A
		runrepeat = true
		repeat
	        BodyPositionPosX = math.min((BodyPositionPosX + 2), 24)
	        game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
			wait(0.3) --[[ Give time for key to go 'up' ]]
		until runrepeat==false

    elseif inputObject.KeyCode == Enum.KeyCode.D then
		currentkeycode = Enum.KeyCode.D
		runrepeat = true
		repeat
      		BodyPositionPosX = math.max((BodyPositionPosX - 2), -16)
      		game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
			wait(0.3)
		until runrepeat==false
    elseif  inputObject.KeyCode == Enum.KeyCode.W then
		currentkeycode = Enum.KeyCode.W
		runrepeat = true
		repeat
	        BodyPositionPosY = math.min((BodyPositionPosY + 2), 24)
       		game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
			wait(0.3)
		until runrepeat==false
    elseif inputObject.KeyCode == Enum.KeyCode.S then
		currentkeycode = Enum.KeyCode.S
		runrepeat = true
		repeat
	        BodyPositionPosY = math.max((BodyPositionPosY - 2), 0)
	        game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
			wait(0.3)
		until runrepeat==false
    end
end

function onKeyUp(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == currentkeycode then
		runrepeat=false
		currentkeycode = nil
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)
game:GetService("UserInputService").InputEnded:connect(onKeyUp)

If you need further help, PM me on ROBLOX:

https://www.roblox.com/messages/compose?recipientId=18982229

~ Taryo

1 comment

0

Thank you :)

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