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!
6 comments
You should really indent your code and space out your operators.
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.
@incapaxian If what you're saying is true then it wouldn't madder if I used ContextActionService, would it? Also why use ContextActionService?
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.
Okay thanks, I'll give it a try.
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.