Trying to connect Keycode table to userinputservice inputbegan

Hi,

How do I connect a table full of keycodes in this certain way?

local Keys = {
	Forward = {Enum.KeyCode.W, Enum.KeyCode.Up, Enum.KeyCode.ButtonY},
	Backward = {Enum.KeyCode.S, Enum.KeyCode.Down, Enum.KeyCode.ButtonA},
	Left = {Enum.KeyCode.A, Enum.KeyCode.Left, Enum.KeyCode.ButtonX},
	Right = {Enum.KeyCode.D, Enum.KeyCode.Right, Enum.KeyCode.ButtonB},
};

self.connections:Connect(UserInputService.InputBegan, function(input: InputObject, gameProcessedEvent: boolean) 
	if gameProcessedEvent then return end

	-- this obviously doesnt work, just wanted to show
	-- how it would look like
	if input.KeyCode == Keys.Forward then

	end

	if input.KeyCode == Keys.Backward then

	end	

	if input.KeyCode == Keys.Left then

	end

	if input.KeyCode == Keys.Right then

	end	
end)
2 Likes

you can use table.find() for this!

example

local Keys = {
	Forward = {Enum.KeyCode.W, Enum.KeyCode.Up, Enum.KeyCode.ButtonY},
	Backward = {Enum.KeyCode.S, Enum.KeyCode.Down, Enum.KeyCode.ButtonA},
	Left = {Enum.KeyCode.A, Enum.KeyCode.Left, Enum.KeyCode.ButtonX},
	Right = {Enum.KeyCode.D, Enum.KeyCode.Right, Enum.KeyCode.ButtonB},
};

self.connections:Connect(UserInputService.InputBegan, function(input: InputObject, gameProcessedEvent: boolean) 
	if gameProcessedEvent then return end

	-- this obviously doesnt work, just wanted to show
	-- how it would look like

	if table.find(Keys.Forward, input.KeyCode) then

	end
end)
2 Likes