Multiple Keys can be pressed simultaneously?

Hello, just trying to create some attacks and abilities to work when I press certain keys, but unfortunately multiple can be fired at the same time; T and Y for example which will make me run and punch which I don’t want. It DOES work when I press one at a time–meaning another key wont trigger until the other is finished. I’ve tried placing my debounce in different ways.
Here are some examples that didn’t work.
NOTE: “enabled” is a bool value in the player’s backpack, and I’m firing a remote function to change its value. i made it this way so there could be a way to access when a player can press keys because certain attacks will be able to enable/disable this )

  1. I’ve tried enabling before input checks, and then disabling at the bottom
  2. I’ve tried checking for enabled conditions within input checks, and then enabled and disabled within them accordingly



EDIT: I’ve also tried making all the other input conditions as “elseif”

Much help is appreciated.

2 Likes

Is enabled a bool value on the server? If I had to guess, I would say client to server latency is the culprit for both situations.

This code is fundamentally wrong right now because you can’t debounce only on the client. If you do that there’s no security, because an exploiter can ignore the debounce and send the attack message as much as they want. You have to send when to start the actions only, and do the verify the debounce timing on the server as well.

Here’s what you need. Client:

local bebounce = false
function onAction(actionName)
    if debounce then return end
    debounce = true
    DoActionRemote:InvokeServer(actionName)
    doAction(actionName)
    debounce = false
end

Server:

local debounce = {}
function onInvoked(player, actionName)
     if debounce[player] then return end
     debounce[player] = true
     doTheStuff(player, actionName)
     debounce[player] = nil
end
2 Likes

“enabled” is assigned to player.Backpack.enabled, which is a boolvalue.

I designed it to be this way so that I could have control on whether or not a player can use their attacks or abilities–>so that certain attacks could disable or enable it (via the server accessing their value and turning it off/on)

I suggest using ContextActionService instead.

Ah, so in the server script at the beginning of whichever ability was invoked, check for the player’s enabled value and then fire along?
Thankfully since the local script will wait until the invoke on the server finishes running its code, it’ll debounce on the server and still be timed back in the client, I assume.
image

Would this still fix the simultaneous issue, though?

Take as example this script i made, it will activate the function when W is pressed twice in a limited time.

It seems like everyone is over complicating things, all you need to do is merge all those conditional statements into a big one as well as add a cooldown.

local lastCast = tick() - 100

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
if tick() - lastCast < 1 then return end -- change 1 to whatever interval you want between each input
-- your other ifs
if Input.KeyCode == Enum.KeyCode.T then
lastCast = tick()
 -- code
elseif Input.KeyCode == Enum.KeyCode.Y then
lastCast = tick() -- updates the last cast

end

end)
2 Likes

Is the solution here to put a timing system so that theres a delay between when the function of inputs are registered? Also, why exactly does it allow me to press them together in the first place? I have my assumptions but since I tested them and they failed, I’m wrong about something here.

Why don’t you use a priority level? Like:

if keyPressed then
elseif otherKeyPressed then
end

keyPressed has priority over otherKeyPressed, place the debounce and then it should work.
Hell, also add waits in the functions that have lower priority to make sure that a function that has higher priority doesn’t fire.

so like:

local function x()
game:GetService("RunService").Heartbeat:Wait()
--check that other function isn't running.
--COde
end

--in UserInput Began
if keyPressed then
elseif otherKeyPressed then
    x()
end

All you need to do is add as many waits for as far down the the priority chain. So a first priority has 0 waits, while a third priority (Like another key press after the otherKeyPress) has 2 wait statements.

As we know, code runs very quickly. What’s happening is as you press every key on your keyboard the listener / event is picking up all of the events happening which runs the InputBegan multiple times. The timing of you pressing 2 buttons will not be perfect and chance are never will be. I’ve simply added a little cooldown on when the code will run. The event is still being signal to run the code, there is just a line that blocks it from going all the way through.

Unfortunately, it didn’t work, I’m still able to press both T and Y at the same

Did you make sure to set reset the last time you pressed? Are you sure you don’t have more InputBegan events connected? Did you try to increase the cooldown?

Reset the last time you pressed?
Heres what I did: I put “local lastCast = tick() - 100” at the beginning of the UserInputBegan, and “lastCast = tick()” beneath each if statement

The only thing I havent tried is rewriting every if statement into “elseif”, which’ll force me to move all of the variables I organized above their if-statements to be pushed all the way up top-- which i’ll simply have to do if it fixes the problem. I still dont feel as though it’ll work :confused: And I’m not sure what changing the interval is for? I cant time sync these manually because each ability does its own thing on its own time and it would be a HUGE hassle; I just want everything else to be ready once something else is done

Well it’s worth a shot, another thing you can do is print tick() - lastCast to see the timing between and then manually check if it’s larger or not then a value

Yep, didn’t work as espected; the tick addition didn’t do much and neither did adding elseif.

Could you print tick() - lastCast each time and show me the output?

image

I know a game that simply uses keydown and this debounce
"if enabled then return end
enabled = true


enabled = false"
and it doesnt have this issue

Well I personally prefer to use tick() but you are free to use that method as well.

I would have to rewrite and reupdate everything over something unsupported including some keys that I use for holding and I would have to go through another loophole trying to figure it out, which is why I simply want to figure out how I can do this with UserInputBegan…

unfortunately, the “tick()” method and having “elseif” for every other input hasn’t worked…
Also, I don’t understand why “Keydown” would work and not this

EDIT: trying keydown, still having same issue