Combo Reset After a certain time

I am trying to make a combat that when you don’t click in like 1.5 seconds, the combo resets. I have tried looking on other fourms, but I can’t seem to figure out how to do this. I have tried to do this myself, but combo won’t reset after certain amount of time.

local ServerEvent = script.Parent:FindFirstChild("ServerEvent")
local UserInputService = game:GetService("UserInputService")
local tool = script.Parent

local idleAnim = tool:WaitForChild("CombatIdle")
local blockAnim = tool:WaitForChild("Block")
local combo1 = tool:WaitForChild("Combo1")
local combo2 = tool:WaitForChild("Combo2")
local combo3 = tool:WaitForChild("Combo3")
local hitAnim = tool:WaitForChild("GettingHit")

local equipped = false
local canAttack = true

local combo = 0
local timeUntilComboResets = 1.5
local lastTimeClicked = tick()
local PunchTime = 0.3


tool.Equipped:Connect(function()
	equipped = true
	character = tool.Parent
	isPlayerBlocking = character:FindFirstChild("isPlayerBlocking")
	humanoid = character:FindFirstChild("Humanoid")
end)

tool.Activated:Connect(function()
	if equipped and isPlayerBlocking.Value == false and canAttack == true then
		combo = combo + 1
		print(combo)
		if combo == 1 then
			combo1Track = humanoid:LoadAnimation(combo1)
			combo1Track:Play()
			canAttack = false
			wait(0.3)
			canAttack = true
		elseif combo == 2 then
			combo2Track = humanoid:LoadAnimation(combo2)
			combo2Track:Play()
			canAttack = false
			wait(0.3)
			canAttack = true
		elseif combo == 3 then
			combo1Track:Play()
			canAttack = false
			wait(0.3)
			canAttack = true
		elseif combo >= 4 then
			combo3Track = humanoid:LoadAnimation(combo3)
			combo3Track:Play()
			combo = 0
			canAttack = false
			wait(0.3)
			canAttack = true
		end		
	end
	**local CurrentclickTime = tick() - lastTimeClicked**
**	print(CurrentclickTime) **
**	if CurrentclickTime >= CurrentclickTime + timeUntilComboResets then**
**		combo = 0**
**		print(CurrentclickTime)**
**		print("Combo Reset")**
**		lastTimeClicked = tick()**
**	end**
end)

tool.Unequipped:Connect(function()
	combo = 0
	equipped = false
	character = nil
	isPlayerBlocking = nil
	humanoid = nil
end)

1 Like
7 Likes

Thank you for the help. It works! How did you come up with the solution and can you explain it to me so I can get a better understanding of it(you can explain if you want you don’t have to)

1 Like

you define the variable lastTimeClicked, which you already did. so then you find the time difference between the lastTimeClicked and the present by doing tick() - lastTimeClicked. if it is greater than 1.5 seconds, then you can reset the combo by setting combo = 0 AKA your default combo variable. then you update the lastTimeClicked variable by making it equal to tick() after the “if” statement because it won’t be of any use if you update it before the if statement. and you should make this at the beginning of the event, so the next lines in the event will take affect upon the activation of the if statement. if you put it at the end, then it will attack THEN reset the combo, which you don’t want. so you put it at the beginning to make it reset THEN attack.

3 Likes

oooh I understand it way better now. Thanks for the explanation

1 Like