Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you make a keyboard input "automatic"?

Asked by 9 years ago

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

01local BodyPositionPosX = 8
02local BodyPositionPosY = 1
03function onKeyPress(inputObject, gameProcessedEvent)
04    if inputObject.KeyCode == Enum.KeyCode.A then
05        BodyPositionPosX = math.min((BodyPositionPosX + 2), 24)
06        game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
07 
08    elseif inputObject.KeyCode == Enum.KeyCode.D then
09        BodyPositionPosX = math.max((BodyPositionPosX - 2), -16)
10        game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
11 
12    elseif  inputObject.KeyCode == Enum.KeyCode.W then
13        BodyPositionPosY = math.min((BodyPositionPosY + 2), 24)
14        game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
15 
View all 23 lines...
0
Use input ended event User#5423 17 — 9y
0
I tried switching it and it only activated when you let go of the button not automatic gaberdell 71 — 9y

1 answer

Log in to vote
1
Answered by
Scarious 243 Moderation Voter
9 years ago

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!):

01local BodyPositionPosX = 8
02local BodyPositionPosY = 1
03local runrepeat = false
04local currentkeycode
05function onKeyPress(inputObject, gameProcessedEvent)
06    if inputObject.KeyCode == Enum.KeyCode.A then
07        currentkeycode = Enum.KeyCode.A
08        runrepeat = true
09        repeat
10            BodyPositionPosX = math.min((BodyPositionPosX + 2), 24)
11            game.Workspace.Part.BodyPosition.position = Vector3.new(BodyPositionPosX,BodyPositionPosY,19)
12            wait(0.3) --[[ Give time for key to go 'up' ]]
13        until runrepeat==false
14 
15    elseif inputObject.KeyCode == Enum.KeyCode.D then
View all 50 lines...

If you need further help, PM me on ROBLOX:

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

~ Taryo

0
Thank you :) gaberdell 71 — 9y
Ad

Answer this question