######I expected this to be another... "You're doing this wrong so do this like this because of this", but I also wanted to attempt another method in input key-sequences.
One of the problems to your code is how you're using a second InputBegan event inside of an existing one. This causes it to create more connections each time the if-checks get to that part (Line 32).
#####If we were to simplify your code down to this to analyze it better:
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
UIS.InputBegan:Connect(function(input2)
print(input2.KeyCode)
end)
end
end)
The above creates InputBegan events everytime you press the E key. If you click on another key at the same time it would print that message once. Do this same thing a few more times and it'll have multiple events triggering this print statement.
I want you to learn about the :IsKeyDown() method which lets us know if a previous key is being held down. Just incase the mechanic you're trying to achieve can use this. This is a function of UserInputService and requires the KeyCode for it to look for.
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
print("SHIFT + E fired")
else
print("Only E was pressed")
end
end
end)
Just by reading the above code, if only the E key is pressed and the LeftShift key is held down beforehand then it will print the first message.
On the topic of key sequences. You can have some if-checks to see which was the last time and key in which input was given.
#####Simply done as:
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if tick() - sinceLastInput <= 0.5 then
if lastInputKey == Enum.KeyCode.R then
print("Combo R into E")
end
end
sinceLastInput = tick()
lastInputKey = input.KeyCode
end
if input.KeyCode == Enum.KeyCode.R then
if tick() - sinceLastInput <= 0.5 then
if lastInputKey == Enum.KeyCode.E then
print("Combo E into R")
end
end
sinceLastInput = tick()
lastInputKey = input.KeyCode
end
end)
The prints fire if the time interval between these two keys were under half a second. You'll need to change the Enums to your desired keys obviously.
I wanted to get IsKeyDown() somehow used in this but I couldn't really see the use case. I just wanted to share these two methods just incase you're desiring a certain mechanic.
###Full code:
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local sinceLastInput = tick()
local lastInputKey = nil
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if tick() - sinceLastInput <= 0.5 then
if lastInputKey == Enum.KeyCode.R then
print("Combo R into E")
end
end
sinceLastInput = tick()
lastInputKey = input.KeyCode
end
if input.KeyCode == Enum.KeyCode.R then
if tick() - sinceLastInput <= 0.5 then
if lastInputKey == Enum.KeyCode.E then
print("Combo E into R")
end
end
sinceLastInput = tick()
lastInputKey = input.KeyCode
end
end)
######Let me know if you need anything explained or if I had missed something.
1 comment
Thanks!