Is there a way to require everything in a table for a function to run?


Achieve

I’m trying to create a function that when triggered, will require everything in a table to play the function; example, my FixCam function.

Issue

The issue is that it works with both buttons separately and I need it to work only when all the buttons are pressed at the same time.

Solutions

I have no solutions for this issue.


Script

local Functions = {
	["FixCam"] = {
		KeybindCodes = {Enum.KeyCode.LeftShift, Enum.KeyCode.F},
		Description = "This function will fix a players camera.",
		Function = function()
		game.Workspace.CurrentCamera:Destroy()
		wait(0.05)
		game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		game.Workspace.CurrentCamera.CameraType = "Custom"
	end}
}
----------------[Main Script]----------------
UIS.InputBegan:Connect(function(KeyPressed)
	if UIS:GetFocusedTextBox() == nil then
		if Equipped == true then
			for _,Function in pairs (Functions) do
				for x,KeyCode in pairs (Function.KeybindCodes) do
					if KeyPressed.KeyCode == KeyCode then
						print(Function)
						Function.Function() 
					end
				end
			end
		end
	end
end)

Thank you to anyone who can help.

1 Like

Are you trying to make a keyboard shortcut sort of system? In this case LeftShift then F

In a sense yes, however it’s connected to a tool and it’s connected to a tool for many different reasons.

So you couldn’t do f then left shift but have to do it in the order of the table, left shift then f?

The issue is, no matter which Keybind I hit… it triggers. I need it to figure out that both of the keybinds are being pressed at the same time. Plus, I don’t want there to have to be an order.

Oh okay, so detecting if BOTH keys are currently being pressed at the same time?

Yes, that is what I need… if I can figure that out, i’ll release my system to the public as I don’t see a basic Mutli-Keybind system anywhere.

local UIS = game:GetService("UserInputService")
local Functions = {
	["FixCam"] = {
		KeybindCodes = {Enum.KeyCode.LeftShift, Enum.KeyCode.F},
		Description = "This function will fix a players camera.",
		Function = function()
			game.Workspace.CurrentCamera:Destroy()
			wait(0.05)
			game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
			game.Workspace.CurrentCamera.CameraType = "Custom"
		end}
}
----------------[Main Script]----------------
UIS.InputBegan:Connect(function(KeyPressed)
	if UIS:GetFocusedTextBox() == nil then
		for _,Function in pairs (Functions) do
			local AllPressed = true
			for x,KeyCode in pairs (Function.KeybindCodes) do
				if not UIS:IsKeyDown(KeyCode) then
					AllPressed = false
					break
				end
			end
			if AllPressed then
				Function.Function()
			end
		end
	end
end)
1 Like