Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does UserinputService not work for left shift in my local script?

I have a local script that uses UserInputService in StarterPlayerScripts and it works with the "J" and "K" keys, but does not work for the "LeftShift" key. This is my local script:

--Variables
local Player=game:GetService("Players").LocalPlayer
local Character=Player.Character or Player.CharacterAdded:Wait()
local Humanoid=Character:WaitForChild("Humanoid")
local UIS=game:GetService("UserInputService")

--Key(s) Down
UIS..InputBegan:Connect(function(Key,gameProcessedEvent)

--J
if Key.KeyCode==Enum.KeyCode.J then
print("J was pressed!")
end

--K
if Key.KeyCode==Enum.KeyCode.K then
print("K was pressed!")
end

--Left shift
if Key.KeyCode==Enum.KeyCode.LeftShift then
print("Left shift was pressed!")
end
end)

--Key(s) Up
UIS.InputEnded:Connect(function(Key,gameProcessedEvent)

--Left Shift
if Key.KeyCode==Enum.KeyCode.LeftShift then
print("Left shift was released!")
end
end)

J and K are working, left shift is not working. I know that it is not "UIS.InputEnded" that is breaking it because "UIS.InputBegan" isn't even working for left shift. I am not going to use any other method for keydown events, so please do not put that in any of your answer(s). Thank you to any answer(s) in advanced.

6 comments

0

You should really indent your code and space out your operators.

0

But more along the lines of the spirit of your inquiry it could be that the LeftShift key is now protected. Not sure though. You can try using ContextActionService.

0

@incapaxian If what you're saying is true then it wouldn't madder if I used ContextActionService, would it? Also why use ContextActionService?

0

Don't know. ContextActionService, as its name implies, is used to react to inputs with different action depending on the context. Don't see why you shouldn't give it a shot.

0

Okay thanks, I'll give it a try.

0

I've never used ContextActionService in a script before though. Could you please put an example down in an answer for me? I'll make sure to mark it as the answer if it works.

1 answer

You don't have to write two different Statements to reenact as if you were holding down a key, UserInputService has an Event that covers this up for us, simply use

UserInputService.InputBegan:Connect(function(UserInput, IsTyping)
   if (UserInput:IsKeyDown(Enum.KeyCode.LeftShift) and not isTyping) then
      print(game.Players.LocalPlayer.Name.." is holding the '"..UserInput.."' key!")
   end
end)

Although you said you weren't going to use other methods, I do suggest taking a look at this.

For ContextActionService, this is what it should look like, I'm not too much of a wiz on this Service but I know enough to show an example, I most likely wont be able to provide a proper explanation, so please see https://developer.roblox.com/api-reference/class/ContextActionService for more information!

local ContextActionService = game:GetService("ContextActionService")
local Player = game:GetService("Players").LocalPlayer
local function ResetCharacter()
   local Character = Player.Character or Player.CharacterAdded:wait()
   local Head = Character:FindFirstChild("Head")
   if (Head) then
      Head:Remove(); wait(1); Player:LoadCharacter()
   end
end
ContextActionService:BindAction("ResetButton", ResetAvatar, true, "R")
ContextActionService:SetPosition("ResetButton", UDim2.new(Vector2, Vector2))
ContextActionService:SetImage("ResetButton", "bxassetid")

Most Users prefer or recommend this Service over UserInputService, yet honestly, for my personal opinion, I wouldn't, I find it too confusing, I'll try to explain what's going on:

Most see above can be recognized as Indexing, and writing functions. That's exactly what it is no more. Except when it comes to :BindAction(), this is the first step, this BuiltInFunction has four arguments: (Reference name; variable, function; event that occurs when tapped, if the button should be functional or not; boolean, same event occur when "key" is pressed on computer). This ultimately sets up the Button, for those who don't understand what I mean by Button, ContextActionService is a Service built to make mobile devices compatible with computers. This Service creates a button similar to the Jump button seen on mobile devices.

After this we have optional advancements, such as customary positioning, yet remember, the first argument always consists of the reference name. Then theres setting a customary Image for the Button, reading the arguments displayed should be pretty self explanatory.

After this, let the Script run, make sure to test this whilst in Emulation mode.

Hope this helps you out, if so, don't forget to Upvote and Accept this Answer!

3 comments

0

I found out that I had my if statement in an elseif statement and that's why it didn't even give me an error message, because to the system there were no errors it just wasn't setup to detect that I was actually pressing the kay and I overlooked it every time I checked it some how(and I checked it like 10 different times). Sorry for making you go through all this trouble but-

0

The answer you gave me might come in use to another developer who is in need of a different method or way to use UserInputService. I will still accept your answer and up vote if you want. It really would really help someone else that might have a similar problem.

0

Yes please:3 Glad to help you, I sent this to you at 4am, I guess I didn’t see the elseif, good luck with everything else!

This discussion is preserved as it was. New answers, comments, and votes are closed.