I’ll make it brief, since it’s mainly just a Q&A thing and not a big discussion.
I’d like to create a melee weapon. With respect to a cooldown between each swing (let’s say 1 second), I’d also like to make it so that players can chain up attacks when they click quickly (before 0.5 seconds of each click). There should be a maximum amount of times before they exhaust the combo, in which they need to wait for the cooldown to initiate. If they don’t start a combo and only swing once, the cooldown also needs to pass before they can swing again.
I’ve tried searching, but found nothing helpful. The only place I know where to begin with is the % operator, but after that I haven’t the slightest.
Try looking at a generic linked sword script, as they have all or almost all of the features you want in your melee weapon.
(This was just the first result I got when searching for the model) https://www.roblox.com/library/58408342/linked-sword
You could use tick() to compare the last time they clicked with the current time they click, that will help you with figuring out what attack they want to do.
example of tick:
local t = tick()
wait(2)
print(tick() - t) --The time elapsed since t was made
The method you provided me is only half complete. It helps me determine when I should chain up attacks but there’s no debouncing anywhere. I don’t exactly know where to go from there.
i.e.
Attack once, have to wait one second
Click in under 0.5 seconds, second attack plays, combo maximum reached, have to wait one second now
I’ll think about this and see if I can provide example code for you at some point when I have more time, and assuming nobody else gives you something to build off of.
I’ve always liked using a ‘streak’ variable to handle the different attacks, and this could also be used for combo multipliers or whatever. An example of some code that might be helpful:
local COMBO_MAX = 2
local COMBO_MAX_WAIT = 1
local ATTACK_WAIT = 0.5
local Streak = 0
local RequestDebounce = false
local LastAttackTick
local function Attack()
-- Do stuff with Streak (multiplier, choose which animation to play, etc.)
end
local function AttackRequest()
-- Let only one attack request be processed at one time
if RequestDebounce then return end
RequestDebounce = true
local CurrentAttackTick = tick()
local TimeSinceLastAttack = (LastAttackTick and CurrentAttackTick - LastAttackTick) or 0
-- Handles cooldown time (COMBO_MAX_WAIT also increases the time the player has to make an attack request, also)
local CooldownTime = (Streak >= COMBO_MAX and COMBO_MAX_WAIT) or ATTACK_WAIT
if not LastAttackTick or TimeSinceLastAttack > CooldownTime then
-- Reset streak
Streak = 1
else
-- Increment streak
Streak = Streak + 1
end
-- Wait until the attack is ready to be played
if TimeSinceLastAttack <= CooldownTime then
wait(Cooldowntime - TimeSinceLastAttack)
end
LastAttackTick = CurrentAttackTick
RequestDebounce = false
Attack()
end
Logic is untested but that should be roughly what you are looking for.