Why is my keybind script not requiring both keys?


Achieve

I’m simply trying to make a keybind system that goes through a table that has the keybinds needed to be pressed down for something to be triggered.

Issue

The first keybind in the keybinds table does not work and if I use the second keycode in the keybind table, it fires. I need it to require all the keybinds in the table.

Solutions

  • I’ve searched the entire DevForum for post relating to this, no luck;
  • I’ve rewritten the code countless times, no luck;
  • I’ve tried debugging and solving this on my own, no luck.

Script

----------------[Settings]----------------
--[Toggles]--
local AllowFreeCam = true --[This allows people to use a FreeCam mode.]--
----------------[Services]----------------
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
----------------[Variables]----------------
local Equipped = false
local Tool = script.Parent
local Players = game.Players
local Characters = nil
local num = 0
--[Keybinds]--
local FreecamKeybind = {Enum.KeyCode.LeftShift, Enum.KeyCode.P}
local FixcamKeybind = {Enum.KeyCode.LeftShift, Enum.KeyCode.F}
--[Keybind Decoder]--
local function KeybindDecoder(Key,Keybind)
	if UIS:IsKeyDown(Keybind[#Keybind]) then
		print("Completed")
		return true
	end
end

UIS.InputBegan:Connect(function(KeyPressed)
	if UIS:GetFocusedTextBox() == nil then
		if Equipped == true then
			if KeyPressed.KeyCode == Enum.KeyCode.E then
				NextPlayer()
			elseif KeyPressed.KeyCode == Enum.KeyCode.Q then
				PreviousPlayer()
			else
			if KeybindDecoder(KeyPressed,FixcamKeybind) then
				FixCam()
			end
			print("Ran")
			--[[elseif KeyPressed.KeyCode == FreecamKeybind[#FreecamKeybind]	then
				ToggleFreecam()]]
			end
		end
	end
end)
--[Equpped]--
script.Parent.Equipped:Connect(function()
	Equipped = true
	GatherPlayers()
end)
--[Unequipped]--
script.Parent.Unequipped:Connect(function()
	Equipped = false
	DismantlePlayers()
	game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
end)

Thank you to anyone who can help.

1 Like

Hi!

Have you tried using

if UIS:IsKeyDown(Keybind1) == true and UIS:IsKeyDown(Keybind2) == true then

Hello, I would like to prevent extra code. I feel there is another way around this situation without needing to determine how many keybinds their are.

1 Like

You said you stored keybind combinations in tables, right? You can just use those.
The function below checks if all keys in the keybind are pressed down

local function allKeysDown(keybindtable)
    for i, key in pairs(keybindtable) do
        if UIS:IsKeyDown(key) == false then
            return false
        end
    end
    return true
end

That’s all I have, sorry if it didn’t help :slightly_frowning_face:

2 Likes