Multiple Keys can be pressed simultaneously?

I think a simple fix to your issue would be to use IsKeyDown. You could do something like this to your if statements:

local HoldingT = UserInputService:IsKeyDown(Enum.KeyCode.T)
local HoldingY = UserInputService:IsKeyDown(Enum.KeyCodeY)
local HoldingU = UserInputService:IsKeyDown(Enum.KeyCode.U)

if HoldingT and not (HoldingY and HoldingU) then
   --run code
elseif --you get the point
end
1 Like

and i would have to type this for EVERY input? there’s at least 10+ more…

You could copy and paste it, but yes, you would have to type this again (unless you can make it into a function).

and are the elseif’s necessary?

The elseifs are necessary for priority. You can also just do if statements, but I would rather do elseif because some of your issue might be because you’re not using elseifs.

As @zQ86 said, you could very well create a function for this:

Originally misunderstood the OP, Edited :grin:

local function  OtherKeysDown(Exception, ...)
	local Keys = ...
	  if typeof(Keys) ~= "table" then
		Keys = {...}
	end
	local inputs = {}
	for index,key in pairs(Keys) do
		inputs[index]  =  UIS:IsKeyDown(key)
	end
	 if table.find(inputs, true) and UIS:IsKeyDown(Exception) then
	    return true
	 elseif UIS:IsKeyDown(Exception) then
     return false
  end
end

and all of this is necessary to enable the userinput to not activate multiple keys simultaneously…?

i misunderstood your issue originally( i have now edited my post to not confuse future readers), but after re-reading a bit i now understand. if you wanted to you can still use the KeyDown function if you wanted it just needs a little editing, it’s not necessary, like suggested previously in this thread you can use Userinputservice and utilize KeyDown, Tick or even to just some boolen logic to see if a given amount of keys are not being pressed down. There are many ways to accomplish or fix your particular problem.

(i`m just talking about checking if keys are being pressed simultaneously, not about other potential errors in your code)

if you still wanted to here’s a function that should check if the others keys specified are not being pressed down:

local function  OtherKeysDown(Exception, ...)
	local Keys = ...
    local keyinputs = {}
	
	  if typeof(Keys) ~= "table" then
		Keys = {...}
	end
	
	for index,Key in pairs(Keys) do
		table.insert(keyinputs, Key)
	end
		for index, value in pairs(Keys) do
               if value == Exception then
			table.remove(keyinputs, index)
		end
	end
	
	local inputs = {}
	for index,key in pairs(keyinputs) do
		inputs[index]  =  UIS:IsKeyDown(key)
	end
	 if table.find(inputs, true) and UIS:IsKeyDown(Exception) then
	    return true
	 elseif UIS:IsKeyDown(Exception) then
     return false
  end
end

So using this function would be used in a loop and it would look something like this:

local Keys = {num.KeyCode.A, enum.KeyCode.B}

while true do
  wait(.1)
  if  OtherKeysDown(enum.KeyCode.A, Keys) == false  then
          print("Only A is being Pressed, Not B")
    end
end
1 Like

This looks really interesting. Let me try to digest this

  1. The “OtherkeysDown” function creates a table of the keys passed into it. If it’s not already within a table, then we’ll put all variables of … into one
    image
  2. Next, if the key youre pressing happens to be in the table, it gets removed
    image
  3. Next we create a table for our inputs by looping thru Keys and assigning those entries into the new one with “IsKeyDown”
    image
  4. This next block is where I get a bit confused.
    The next condition checks if the inputs are being pressed AND the exception… ?
    Otherwise, if the only thing being pressed is the exception, give back a false value?
    image
    EDIT: I see, when you are calling the function, you’ll get either a true value from when all inputs are pressed including the exception, or only false when it’s ONLY the exception pressed. I think.
    image

Still sorting it out…
Oh, and I tested the “IsKeyDown” some and it kinda works! Found a debounce issue somwhere but we’ll see.

1 Like

Ok so I’ll try to break it down a little bit more:

if table.find(inputs, true) and UIS:IsKeyDown(Exception) then  

This is used to check if any key that is in the input table is being pressed and if the Exception key is pressed, indicating that any amount keys are being pressed simultaneously or at the same time that the Exception is also bing pressed down. If both of these conditions are true then return true, that keys are being pressed down simultaneously.

elseif UIS:IsKeyDown(Exception) then
     return false
  end

Now, if the Exception is being pressed down but no other keys are being pressed down then return fasle, indicating that only the Exception is being pressed.

table.remove(Keys, index)

The reason its index instead of value is because the table.remove method calls for an index, if you really wanted to you could use table.find with table.remove as table.find returns the index it found the specified value at or nil if it’s not found.

1 Like

darn, im running and punching. i guess it didn’t work, when trying all the inputs as “if”, and even using elseif
image

Can you provide your updated/current script?

image

oops, made a mistake, the reason it wasn’t working is because it was removing(table.remove) the actual input from the Keys Table:

local Keys = {num.KeyCode.A, enum.KeyCode.B}

instead of from a new table, so this should work:

local function  OtherKeysDown(Exception, ...)
	local Keys = ...
    local keyinputs = {}
	
	  if typeof(Keys) ~= "table" then
		Keys = {...}
	end
	
	for index,Key in pairs(Keys) do
		table.insert(keyinputs, Key)
	end
		for index, value in pairs(Keys) do
               if value == Exception then
			table.remove(keyinputs, index)
		end
	end
	
	local inputs = {}
	for index,key in pairs(keyinputs) do
		inputs[index]  =  UIS:IsKeyDown(key)
	end
	 if table.find(inputs, true) and UIS:IsKeyDown(Exception) then
	    return true
	 elseif UIS:IsKeyDown(Exception) then
     return false
  end
end

Also this function would work better in a loop or using UIS.InputChanged, otherwise it will likely not work correctly at all.

if you are using UIS.InputChanged i would move the debounce to the KeysDown if statement because you don’t want the debounce Affecting the KeyDowns function itself so i would do something like this (you can play around with it if you want to):

UIS.InputChanged:Connect(function(input)
	  if  OtherKeysDown(Enum.KeyCode.T, Keys) == false  then
			 if not enabled.Value then
			enabled.Value = true
                  print("T is Pressed")
			wait(1)
			 enabled.Value = false
		 end 
    end
end)

On another note use Connect instead of connect as it is depreciated

Im using UserInputBegan–changed seems like it’ll confuse me-- and it’s working perfectly fine. :laughing:
One issue I dug up is that if I press any of WASD and one of the keys such as T or Y, the server function code (within the input if statement) will fire twice…