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
The move they want to do is called fireball
They press the key to do fireball and they do an animation signituring that they are getting ready to do an ability
They do a key combination for the fireball and each key they press for the combination, they do an animation.
When they get the combination correct then they release the fireball dealing damage to their opponent
SECOND SENARIO
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
They change their mind and dont want to do the ability anymore and press the key again to get rid of the animation.
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
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)
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)
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
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