Up is printed whenever ANY key is lifted, and Down is never printed, even when I press the right keys. Does anyone know my problem?
01 | local character = script.Parent |
02 | local Mouse = game.Players:GetPlayerFromCharacter(character):GetMouse() |
03 | local speed = 8 |
04 |
05 | Mouse.KeyDown:connect( function (key) |
06 | if key = = Enum.KeyCode.RightShift then |
07 | game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed + 8 |
08 | print ( "Shift Down" ) |
09 | end |
10 | if key = = Enum.KeyCode.X then |
11 | game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed - 9 |
12 | print ( "X Down" ) |
13 | end |
14 | end ) |
15 |
Because Mouse.KeyDown returns a string value, if my mind serves me right. InputBegan returns an InputObject with a KeyCode value.
Instead, you should use InputBegan, like so.
01 | game:GetService( 'UserInputService' ).InputBegan:Connect( function (KeyPressed, GameProcessedEvent) |
02 |
03 | if ( not GameProcessedEvent) then --We have this here because if GameProcessedEvent is true, then that means a player is inside of a Gui with modal. We don't want players executing actions while typing or anything like that. |
04 |
05 | --I also recommend redefining Key to it's keycode if you're not using any other property (UserInputType and all that) |
06 | KeyPressed = KeyPressed.KeyCode |
07 | if (KeyPressed = = Enum.KeyCode.X) then |
08 | xFunctionHere() |
09 | elseif (KeyPressed = = Enum.KeyCode.Y) then |
10 | yFunctionHere() |
11 | elseif (KeyPressed = = Enum.KeyCode.Z) then |
12 | zFunctionHere() |
13 | end |
14 | end |
15 | end ) |
The Connect function requires a function to call when they signal is fired. We take care of that by making an unnamed new function inside of the first argument. You can also fill in a function name by defining a function outside of the Connect, and giving the proper two arguments.
1 | function Key(Key, Event) |
2 | if ( not Event) then |
3 | print ( 'Key pressed!' ) |
4 | end |
5 | end |
6 |
7 | game:GetService( 'UserInputService' ).InputBegan:Connect(Key) |
I typically use the first example because I code without thinking (and it works wonders, but I definitely don't recommend it). It looks a little bit less readable than having the actual function outside of the InputBegan, but it still works, and it's a tad bit quicker in my honest opinion.
Good luck!
I edited your code to fit my needs, and it still doesn't work. Have I done anything wrong? P.S. For some reason it starts me off with 16 speed, then when I lift any key it sets it back to 8, and nothing works again.
01 | local speed = 8 |
02 |
03 | game:GetService( 'UserInputService' ).InputBegan:Connect( function (KeyPressed, GameProcessedEvent) |
04 | if not GameProcessedEvent then |
05 | KeyPressed = KeyPressed.KeyCode |
06 | if KeyPressed = = Enum.KeyCode.RightShift then |
07 | game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed + 8 |
08 | elseif KeyPressed = = Enum.KeyCode.X then |
09 | game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed - 9 |
10 | end |
11 | end |
12 | end ) |
13 |
14 | game:GetService( 'UserInputService' ).InputEnded:Connect( function (KeyPressed, GameProcessedEvent) |
15 | if not GameProcessedEvent then |