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.
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!