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

How do I do when both pressed on UserInputService?

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

✓ Accepted answer
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.

1 comment

0

Thanks so much this works now I understand!

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