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 8 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

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)
0
Use input ended event User#5423 17 — 8y
0
I tried switching it and it only activated when you let go of the button not automatic gaberdell 71 — 8y

1 answer

Log in to vote
1
Answered by
Scarious 243 Moderation Voter
8 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!):

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

0
Thank you :) gaberdell 71 — 8y
Ad

Answer this question