SO i have this script where basically a player need to press R then T then R again to do print the statement, but it wont let me do R again and says a warning saying that the condition was already used. How can I fix that?
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)
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
if t_pressed then
print("R key was pressed first, then pressed T key!")
end
elseif obj.KeyCode == Enum.KeyCode.T then
if r_pressed then
t_pressed = true
-- And so on
end
end
end
end)
This should fix your current problem. You don’t need to check if it was pressed twice like @Valkyrop said since it’s going to get confused, once is enough and should work as you intended.
if obj.KeyCode == Enum.KeyCode.R then
r_pressed = true
elseif obj.KeyCode == Enum.KeyCode.T then
if t_pressed then
print("T pressed again")
end
if r_pressed then
t_pressed = true
end
end
Well I’m not really sure how you could do that, maybe adding each combination into a table and see if it matches with one in the table. For example having a table
local ButtonPresses = {
[1] = {"T","T","R"}
}
local CurrentButttons = {}
if obj.KeyCode = Enum.KeyCode.R then
-- Add R to the CurrentButtons Table
elseif obj.KeyCode = Enum.KeyCode.T then
-- Add T to the table
end
-- If table has 4 indexes try and find it in ButtonPresses table
-- If it finds it do something else it was invalid
Of course not sure whether this idea will work but you may be able to find a way or even come up with a better idea. I’m sure someone can help more with this but this is all I can really think of.
Can cause a confusion. There is literally no need to check twice.
You could instead check if the player pressed Keys in a certain order , for e.g: R,T,R.
[You might need a variable]
I managed to make something like this to do (R,T,T,R) but the last R doesnt work for some reason
local uis = game:GetService("UserInputService")
local r_pressed = false
local t_pressed = false
local t_pressed2 = false
uis.InputBegan:Connect(function(obj) -- when theres an input
if obj.KeyCode == Enum.KeyCode.R then
r_pressed = true
elseif obj.KeyCode == Enum.KeyCode.T then
if t_pressed then
print("T pressed again")
local t_pressed2 = true
end
if r_pressed then
t_pressed = true
end
end
if obj.KeyCode == Enum.KeyCode.R then
if t_pressed2 then
print("Lets do this")
end
end
end)
I hate to break it to you but this is definitely not the approach you want when doing combo keys.
A better way is to store the key values in a string. Like this
local Combo = ""
UserInputService.InputEnded:Connect(function(inputObject,gameProcessedEvent)
if gameProcessedEvent then return end
if inputObject.UserInputType.Name == "Keyboard" then --*See below
Combo ..= inputObject.KeyCode.Name
print(Combo == "RRT")
end
end)
*You can also make a list of all combo keys.
local VALID_COMBO_KEYS = {"R","T","F"}
if table.find(VALID_COMBO_KEYS,inputObject.KeyCode.Name) then
-- Code
end
After a specified time you will want to clear the Combo variable. For exmple after 1 second or when you reach 4 letters. There are a couple ways to do it some better than others.
local KeyHitMarker = time()
local Combo = ""
UserInputService.InputEnded:Connect(function(inputObject,gameProcessedEvent)
if gameProcessedEvent then return end
if inputObject.UserInputType.Name == "Keyboard" then --*See below
if time() - KeyHitMarker > 1 then
Combo = ""
end
Combo ..= inputObject.KeyCode.Name
print(Combo == "RRT")
end
end)
IMPORTANT SIDE NOTE
One problem you will have is that combos that have longer length will override combos that have shorter length. In that case make sure you setup a system that can do this or else you will have more than 1 move playing at any time. e.g.
Combos: QWE, QW, QE
Press Q
Add Q to Combo
Press W
Add W to Combo
Found Combo in Combos
Playing Combo QW
Press E
Add E to Combo
Found Combo in Combos
Playing Combo QWE
-- get the userinputservice
local userInputService = game:GetService("UserInputService")
-- save a table of keys we must press in order
local keys = {
Enum.KeyCode.R,
Enum.KeyCode.T,
Enum.KeyCode.R,
}
-- the current key we must press
local index = 1
-- when a input event begins call this function
userInputService.InputBegan:Connect(function(input)
-- if the input event is not a keyboard event then exit the function early
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
-- check to make sure the correct key was pressed
if input.KeyCode == keys[index] then
-- the correct key was pressed now lets check to see if it was the last key in the table
if index == #keys then
-- this was the last key in the table lets reset the the index so they can press the combo again
index = 1
print("Combo complete!!!")
else
-- we still have not finished the combo lets increment the index and wait for the next key press
index += 1
print("Please press:", keys[index])
end
else
-- the incorrect key was pressed reset the index so they must start the combo from the start
index = 1
print("Combo failed please start again")
end
end)