How to activate abilities using key combinations

Hello, I want to know how to make a script where if a player presses a button to do an ability, they can do a key combination to activate the remote event for the ability. If they change their mind and dont want to activate the ability midway through the key combination, they can just press the button again.(by button, I mean a key on the keyboard). I’m trying to make a system where to activate abilities you have to first have the ability ready the do the specific key combination for that ability. Each ability I will costomize the ability. I’ll give you an example in steps the player has to take

  1. The move they want to do is called fireball
  2. They press the key to do fireball and they do an animation signituring that they are getting ready to do an ability
  3. They do a key combination for the fireball and each key they press for the combination, they do an animation.
  4. When they get the combination correct then they release the fireball dealing damage to their opponent

SECOND SENARIO

  1. The player does the same thing and they press the key for the fireball doing the animation signaturing they are getting ready to do an ability
  2. They change their mind and dont want to do the ability anymore and press the key again to get rid of the animation.

(Keys I will most likely be using)

  1. R
  2. Whatever the key is next to R

I’m assuming you’re having trouble with the logic and not the actual scripting. So I’m not gonna write out any code (Or at least any real code) but I’ll try to show you how you would Generally do something like this.

I would personally first make a variable that records if the player is currently “ready to do an ability” and then I’d have a function that, when R is pressed, goes from a “ready to do an ability” state to a normal state and back if R was pressed again. Something like this

Currently_In_Ability = false

Player Pressed a Letter()
   if Player Presses R then
        Currently_In_Ability = not Currently_In_Ability -- Flips output
        if Currently_In_Ability == false then
             Currently_In_Ability = true
             Start_Animation_And_Whatever()
        else
             Currently_In_Ability = false
             End_Animation_And_Whatever()
        end
   end
end

This would cover the second senario but not the first one. To do that, you’d need to check if the player does the button combination. For now we will assume the button combinations are 1 letter long as this will making scripting this so much easier for now.

Currently_In_Ability = false

Player Pressed a Letter()
   if Player Presses R then
        Currently_In_Ability = not Currently_In_Ability -- Flips output
        if Currently_In_Ability == false then
             Currently_In_Ability = true
             Start_Animation_And_Whatever()
        else
             Currently_In_Ability = false
             End_Animation_And_Whatever()
        end
   elseif Currently_In_Ability == true then
       if Player Pressed T then
            Activate_Ability_A()
             Currently_In_Ability = false
             End_Animation_And_Whatever()
       elseif Player Pressed F then
            Activate_Ability_B()
            Currently_In_Ability = false
            End_Animation_And_Whatever()
       end
   end
end

If you wanted to have serveal keys for the combination for an ability you would have to save every letter pressed in that state in either a table or a string and if that table or string matched a combination of ability, you would fire it. Here’s the devhub article for tables incase you want to know how they work

Hopefully that at least helps

You can use UserInputService

Example code for key combination

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(obj) -- when theres an input
  if obj.UserInputType == Enum.UserInputType.Keyboard then -- If input is from a keybord then
     if obj.KeyCode == Enum.KeyCode.R and obj.KeyCode == Enum.KeyCode.T then -- when key R and key T pressed together then
         print("Key R and Key T pressed at the same time!")
      end
  end
end)

I dont want it so that they have to press it at the same time, but one after the other.

Oh so,

local uis = game:GetService("UserInputService")

local r_pressed = false
local t_pressed = false

uis.InputBegan:Connect(function(obj) -- when theres an input
  if obj.UserInputType == Enum.UserInputType.Keyboard then -- If input is from a keybord then
     if obj.KeyCode == Enum.KeyCode.R then
         r_pressed = true
     elseif obj.KeyCode == Enum.KeyCode.T then
        if r_pressed then
            t_pressed = true
            print("R key was pressed first, then pressed T key!")
            -- And so on
        end
     end
  end
end)

Ok, that works, I’ll test it out later.

How would I add that to the script the other person gave me though?

Also, ive been modifying the script to make different combination like(R,T,T,R)(R,T,R,T)(T,RR,T) and it doesnt seem to be working

Do you know how I can make it so that there are more to the combination?

here the script, it wont let me do R again

local uis = game:GetService("UserInputService")

local r_pressed = false
local t_pressed = false

uis.InputBegan:Connect(function(obj) -- when theres an input
  if obj.UserInputType == Enum.UserInputType.Keyboard then -- If input is from a keybord then
     if obj.KeyCode == Enum.KeyCode.R then
         r_pressed = true
     elseif obj.KeyCode == Enum.KeyCode.T then
        if r_pressed then
            t_pressed = true
				-- And so on
		end
	 elseif obj.KeyCode == Enum.KeyCode.R then
		if t_pressed then
			t_pressed = true
			print("R key was pressed first, then pressed T key!")
			-- And so on
			
        end
     end
  end
end)

it says that R has already been used, but i need it to be R again, please help

image
Add r_pressed = false

image

Add r_pressed = true
and t_pressed = false

the r_pressed and t_pressed just for the demonstration.
You can do something like this.


it still gives me the same error

Theres no need of creating another if condition
you can just do

if obj.KeyCode == Enum.KeyCode.R then
         	if t_pressed then
			       t_pressed = false
			       print("R key was pressed first, then pressed T key!")
             end
         r_pressed = true
 elseif obj.KeyCode == Enum.KeyCode.T then
        if r_pressed then
            t_pressed = true
				-- And so on
		end
end