Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

User Input Service Not Doing Anything at All?

Asked by 6 years ago

I'm trying to create a script that when the left shift key is pressed, it fires a remoteevent to make the player crouch. However, absolutely nothing happens. It doesn't print anything, but I also don't get any errors.

local crouch = game.ReplicatedStorage.Crouch
UIS = game:GetService('UserInputService')

UIS.InputBegan:connect(function(input,processed)
    if not processed then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            print("leftshift pressed")
            crouch:FireService("yes")
        end
    end
end)

UIS.InputEnded:connect(function(input,processed)
    if not processed then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            print("leftshift pressed release")
            crouch:FireService("No")
        end
    end
end)

I also tried the way from the wiki, and it still didn't work. (i already got rid of the code but it was essentially this with a remoteevent)

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
        print("R was pressed")
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

If you are just making the player crouch through animations, you shouldn't need to use RemoteEvents. Even if you did, you have to use :FireServer() not :FireService() to fire the server script. For UserInputService to work it must be in a LocalScript inside somewhere that's individual to each player (e.g. StarterGui which replicates the script to every player). Since animations are one of the few things that replicate to the server with FE, you should be able to do something like this in a LocalScript:

local animation = Instance.new("Animation")
animation.AnimationId = "" --insert id in quotes
local track

game:GetService("UserInputService").InputBegan:Connect(function(key,isTyping)
    if isTyping == false and key.KeyCode == Enum.KeyCode.LeftShift then
        local char = game.Players.LocalPlayer.Character
        if char then
            track = char:WaitForChild("Humanoid"):LoadAnimation(animation)
            track.Looped = true
            track:Play()
        end
    end
end)
game:GetService("UserInputService").InputEnded:Connect(function(key,isTyping)
    if isTyping == false and key.KeyCode == Enum.KeyCode.LeftShift then
        if track then
            track:Stop()
        end
    end
end)
0
Thank you for this! I was super tried while writing the code I didn't even notice I wrote :FireService. I'm very embarrassed but thankful for your amazing answer! BouncingBrenda 44 — 6y
Ad

Answer this question