Shorten egregiously long Userinput Function

I Genuinely have no idea how to shorten this, this is NOT skibidi.

		UserInputService.InputBegan:Connect(function(IsTyping, GPE, Input)
			if IsTyping == true or GPE == true then return end
			
			
			if Input.KeyCode == Enum.KeyCode.W then
				KeyDown = "W"
			elseif Input.KeyCode == Enum.KeyCode.S then
				KeyDown = "S"
			elseif Input.KeyCode == Enum.KeyCode.A then
				KeyDown = "A"
			elseif Input.KeyCode == Enum.KeyCode.D then
				KeyDown = "D"
			elseif Input.KeyCode == Enum.KeyCode.W and Input.KeyCode == Enum.KeyCode.A then
				KeyDown = "W,A"
			elseif Input.KeyCode == Enum.KeyCode.W and Input.KeyCode == Enum.KeyCode.D then
				KeyDown = "W,D"
			elseif Input.KeyCode == Enum.KeyCode.S and Input.KeyCode == Enum.KeyCode.A then
				KeyDown = "S,A"
			elseif Input.KeyCode == Enum.KeyCode.S and Input.KeyCode == Enum.KeyCode.D then
				KeyDown = "S,D"
			elseif Input.KeyCode == Enum.KeyCode.W and Input.KeyCode == Enum.KeyCode.S then
				KeyDown = "W,S"
			elseif Input.KeyCode == Enum.KeyCode.A and Input.KeyCode == Enum.KeyCode.D then
				KeyDown = "A,D"
			elseif Input.KeyCode == Enum.KeyCode.W and Input.KeyCode == Enum.KeyCode.A and Input.KeyCode == Enum.KeyCode.S then
				KeyDown = "W,A,S"
			elseif Input.KeyCode == Enum.KeyCode.W and Input.KeyCode == Enum.KeyCode.A and Input.KeyCode == Enum.KeyCode.D then
				KeyDown = "W,A,D"
			elseif Input.KeyCode == Enum.KeyCode.S and Input.KeyCode == Enum.KeyCode.A and Input.KeyCode == Enum.KeyCode.D then
				KeyDown = "S,A,D"
			elseif Input.KeyCode == Enum.KeyCode.S and Input.KeyCode == Enum.KeyCode.A and Input.KeyCode == Enum.KeyCode.D and Input.KeyCode == Enum.KeyCode.W  then
				KeyDown = "W,A,S,D"
			end
				
		end)
1 Like
local UserInputService = game:GetService("UserInputService")

local keyMap = {
    [Enum.KeyCode.W] = "W",
    [Enum.KeyCode.S] = "S",
    [Enum.KeyCode.A] = "A",
    [Enum.KeyCode.D] = "D"
}

UserInputService.InputBegan:Connect(function(Input, GPE)
    if GPE then return end

    local key = keyMap[Input.KeyCode]
    if key then
        KeyDown = key
    end
end)

this script makes the keyDown variable any possible combination of WASD, so then use ifs to just check if 2 are being held at the same time in different areas

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.