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!):
01 | local BodyPositionPosX = 8 |
02 | local BodyPositionPosY = 1 |
03 | local runrepeat = false |
05 | function onKeyPress(inputObject, gameProcessedEvent) |
06 | if inputObject.KeyCode = = Enum.KeyCode.A then |
07 | currentkeycode = Enum.KeyCode.A |
10 | BodyPositionPosX = math.min((BodyPositionPosX + 2 ), 24 ) |
11 | game.Workspace.Part.BodyPosition.position = Vector 3. new(BodyPositionPosX,BodyPositionPosY, 19 ) |
13 | until runrepeat = = false |
15 | elseif inputObject.KeyCode = = Enum.KeyCode.D then |
16 | currentkeycode = Enum.KeyCode.D |
19 | BodyPositionPosX = math.max((BodyPositionPosX - 2 ), - 16 ) |
20 | game.Workspace.Part.BodyPosition.position = Vector 3. new(BodyPositionPosX,BodyPositionPosY, 19 ) |
22 | until runrepeat = = false |
23 | elseif inputObject.KeyCode = = Enum.KeyCode.W then |
24 | currentkeycode = Enum.KeyCode.W |
27 | BodyPositionPosY = math.min((BodyPositionPosY + 2 ), 24 ) |
28 | game.Workspace.Part.BodyPosition.position = Vector 3. new(BodyPositionPosX,BodyPositionPosY, 19 ) |
30 | until runrepeat = = false |
31 | elseif inputObject.KeyCode = = Enum.KeyCode.S then |
32 | currentkeycode = Enum.KeyCode.S |
35 | BodyPositionPosY = math.max((BodyPositionPosY - 2 ), 0 ) |
36 | game.Workspace.Part.BodyPosition.position = Vector 3. new(BodyPositionPosX,BodyPositionPosY, 19 ) |
38 | until runrepeat = = false |
42 | function onKeyUp(inputObject, gameProcessedEvent) |
43 | if inputObject.KeyCode = = currentkeycode then |
49 | game:GetService( "UserInputService" ).InputBegan:connect(onKeyPress) |
50 | game:GetService( "UserInputService" ).InputEnded:connect(onKeyUp) |
If you need further help, PM me on ROBLOX:
https://www.roblox.com/messages/compose?recipientId=18982229
~ Taryo