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

How do I do when both pressed on UserInputService?

Asked by
Hero_ic 502 Moderation Voter
9 years ago

The function works but only when I take out one of the keys. How do I make it work when both are pressed and not just one because this script does not work unless I take out if input.KeyCode == Enum.KeyCode.W then.

Hope I explained this right

game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.W then
        if input.KeyCode == Enum.KeyCode.LeftShift and action == "none" then
            player.Character.Humanoid.WalkSpeed=21
                action = "sprint"
            end
        end 
    end)

1 answer

Log in to vote
1
Answered by 9 years ago
local shift = false
game:GetService("UserInputService").InputBegan:connect(function(input)
    if action ~= "none" then return end -- If there is a current action then just stop the function
    if input.KeyCode == Enum.KeyCode.W and shift then
        player.Character.Humanoid.WalkSpeed=21
        action = "sprint"
    elseif input.KeyCode == Enum.KeyCode.LeftShift then
        shift = true
    end
end)

game:GetService("UserInputService").InputEnded:connect(function(input)
    if action == "none" then return end -- If there is no current action then just stop the function
    if input.KeyCode == Enum.KeyCode.W then
        player.Character.Humanoid.WalkSpeed=16
        action = "none"
    elseif input.KeyCode == Enum.KeyCode.LeftShift then
        shift = false
    end
end)

What you need to do was set a boolean value 'shift' to false and when pressing LeftShift, set it to true. That is because you tried to check if the input was both W and LeftShift but it can only check that one thing, not all current keys being held. I also added an ended event but if this isn't what you had planned you can remove it.

0
Thanks so much this works now I understand! Hero_ic 502 — 9y
Ad

Answer this question