Basically, I have a tool. The tool has 3 attacks. After attack1 it goes to attack2 then attack3
However, if you take too long it will reset back to the first attack.
I used tick() to try and approach this problem. But this method doesn’t work.
local cooldownTime = -0.5
local lastActivationTime = 0
local status = 1
script.Parent.Equipped:Connect(function()
print("Equipped")
end)
script.Parent.Unequipped:Connect(function()
print("Unequipped")
end)
script.Parent.Activated:Connect(function()
local currentTime = tick()
if currentTime - lastActivationTime >= cooldownTime then
lastActivationTime = tick()
if status == 1 then
status = 2
print("1")
return
elseif status == 2 then
status = 3
print("2")
return
elseif status == 3 then
status = 1
print("3")
return
end
else
print("You need to wait before activating again")
end
end)
I’m pretty bad at math in general, so if anyone can just a basic outline/explain how to do this it would be helpful.
The system for this is actually very simple. You just need to calculate the time between the user’s activation time to the current time and if its higher or equal then reset the combo. Also, instead of checking if the current combo is either 1,2,3… you can use tables and just add the combo by 1. You can also add a cooldown on the every activation that avoids the system being spammed.
I recommend trying to figure out yourself first. But if you’re still quite confused you can review this code.
local Tool = script.Parent
local Combo = 0
local ActivationTime = 0
-- Time until the combo will reset back.
local RESET_TIME = 2
--[[
Time until the player can attack again so that tthe activation won't get spammed.
Alternatively, if you have an animation track that is being played. Use track.Stopped:Wait() .
]]
local WAIT_PER_COMBO = 0.5
local Actions = {
[1] = function()
print("This is combo 1.")
end,
[2] = function()
print("This is combo 2")
end,
[3] = function()
print("This is combo 3")
end,
[4] = function()
print("This is combo 4")
end,
}
function ResetCombo()
Combo = 0
end
function OnActivated()
local timePassed = tick() - ActivationTime
if timePassed >= RESET_TIME then
ResetCombo()
print("Combo has been reset.")
end
-- In cooldown.
if timePassed <= WAIT_PER_COMBO then
print("In cooldown.")
return
end
Combo += 1
local action = Actions[Combo]
if action then
action()
-- action(character, humanoid, etc.)
end
ActivationTime = tick()
if Combo >= #Actions then
ResetCombo()
end
end
Tool.Activated:Connect(OnActivated)