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?
local character = script.Parent local Mouse = game.Players:GetPlayerFromCharacter(character):GetMouse() local speed = 8 Mouse.KeyDown:connect(function(key) if key == Enum.KeyCode.RightShift then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed + 8 print("Shift Down") end if key == Enum.KeyCode.X then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed - 9 print("X Down") end end) Mouse.KeyUp:connect(function(key) if key == Enum.KeyCode.RightShift or Enum.KeyCode.X then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed print("Up") end end)
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.
game:GetService('UserInputService').InputBegan:Connect(function(KeyPressed, GameProcessedEvent) 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. --I also recommend redefining Key to it's keycode if you're not using any other property (UserInputType and all that) KeyPressed = KeyPressed.KeyCode if (KeyPressed == Enum.KeyCode.X) then xFunctionHere() elseif (KeyPressed == Enum.KeyCode.Y) then yFunctionHere() elseif (KeyPressed == Enum.KeyCode.Z) then zFunctionHere() end end 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.
function Key(Key, Event) if (not Event) then print('Key pressed!') end end 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.
local speed = 8 game:GetService('UserInputService').InputBegan:Connect(function(KeyPressed, GameProcessedEvent) if not GameProcessedEvent then KeyPressed = KeyPressed.KeyCode if KeyPressed == Enum.KeyCode.RightShift then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed + 8 elseif KeyPressed == Enum.KeyCode.X then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed - 9 end end end) game:GetService('UserInputService').InputEnded:Connect(function(KeyPressed, GameProcessedEvent) if not GameProcessedEvent then KeyPressed = KeyPressed.KeyCode if KeyPressed == Enum.KeyCode.RightShift or Enum.KeyCode.X then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed end end end)