How do I make it so if the player presses multiple keys in a certain order, they do an attack? Would I have to first do an if statement and then have a tick and then another if statement? I am able to make it so that one key is used when using a move. But I want to use multiple for one move.
I would do as follows:
Create a 2-dimensional array that includes both the key that must be pressed, as well as the function that you'd like to fire off whenever the player presses said key.
local Combo = { [1] = {Enum.KeyCode.K, ComboPart1}; [2] = {Enum.KeyCode.Z, ComboPart2}; -- etc. }
Create a counter that will track where you are in the array.
local Counter = 1
Create a variable for the key that you're currently on
KeyToPress = Combo[Counter][1]
Now listen for when KeyToPress is pressed, and when it is, increment your counter, fire off its respective function, and set KeyToPress to the new key (using the same snippet from above).
UserInputService.InputBegan(function(Input, GameProcessed) if(not GameProcessed) then if(Input.KeyCode == KeyToPress) then Combo[Counter][2]() -- Fire combo function Counter = Counter + 1 KeyToPress = Combo[Counter][1] end end end)
In your listening function, whenever a button is pressed, set a variable to tick() and just check that against your cooldown every time you press a key. If it's more than the cooldown, simply set your counter to 1 instead of incrementing it.
StartOverTime = 5 ButtonPressedTime = tick() UserInputService.InputBegan(function(Input, GameProcessed) if(not GameProcessed) then if(Counter ~= 1) then if(tick()-ButtonPressedTime > StartOverTime) then Counter = 1 KeyToPress = Combo[Counter][1] end end if(Input.KeyCode == KeyToPress) then Combo[Counter][2]() -- Fire combo function Counter = Counter + 1 KeyToPress = Combo[Counter][1] end end end)
Note: You may want to add an if-statement that allows you to loop the counter granted it reaches the end of the table.
Another note: You may need a variable to keep track of which combo you're currently executing, so that pressing keys doesn't cause you to start another attack on accident.
Disclaimor: This code might have a couple of simple errors in it (I made a similar system a while back, but I haven't tested this particular code), and there are a few things you can do to clean it up (i.e. making a function that both increments the counter and changes KeyToPress rather than typing out both every time), but I think the main idea is there.
I hope this helps.