✓ Accepted answer
The wiki has a really nice article regarding debounces, I recommend giving it a read.
The below will do as I believe you intend. If you use one move, it'll wait until it finishes executing before you're able to use it again. However, the other move will still be available for use.
local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait();
local UIS = game:GetService("UserInputService")
local attacks = {
[Enum.KeyCode.Q] = function()
local Part = Instance.new("Part")
Part.Parent = workspace
Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
wait(3)
end;
[Enum.KeyCode.E] = function()
local Part = Instance.new("Part")
Part.Parent = workspace
Part.CFrame = char.Head.CFrame * CFrame.new(0,0,-5)
wait(3)
end;
}
local function ForceDebounce(func)
local debounce = false;
return function(...)
if(not debounce) then
debounce = true;
func(...);
debounce = false;
end
end
end
local function ForceDebounceAllAttacks()
for i, v in pairs(attacks) do
attacks[i] = ForceDebounce(v);
end
end
ForceDebounceAllAttacks();
UIS.InputBegan:Connect(function(Input)
for i, v in pairs(attacks) do
if(Input.KeyCode == i) then
v();
end
end
end)
- Note that you'll be able to use both moves simultaneously.
Let me know if you have any questions.
Edit: Added explanation
I added a few comments to make it a bit easier to understand.
The basic idea is that, rather than writing out some long-winded series of if-statements in your InputBegan connection, I just used a table, wherein the indices are keys you can press, and the values are the functions that should execute when those keys are pressed. I then forced a debounce on each of them (using a function that takes advantage of closures). From there, it's a simple matter of running through every attack in the table through a generic for loop, checking against the input, and then executing the attack if the key was pressed.
I hope this helps shed a little more light for what's going on in the code above, I apologize for the added confusion.
2 comments
You shouldn't create a whole different for a different key code, use 'elseif', because currently you're creating new connections each time a button is pressed.
incapaz, how would i use elseif in this script?