Reset a Sword Combo to 0

Hi, I am trying to code a system for a game’s sword combat.
The sword is meant to cycle through three different animations:
1: The initial swing (only play this on combo start)
2: Combo swing 1
3: Combo swing 2

I know how to set up:

  • Changing the combo value
  • Looping back to combo = 2 when the combo is continued
  • Applying animations to combo value

However, I am having some trouble in resetting the combo back to zero. What I am trying to make is that if the player does not make an input within a set window after an attack, the combo resets to 0.

I have looked through several tutorials and devforum posts, and for some reason, those do not work when I try to use them. I am currently using a timer module recommended for combos, however that has several conflicts with my other scripts, more specifically the ones involving a dash feature.
I have also tried looking into an open source sword game (specifically this one: Sword Combat Test - Roblox) and while I can see a combo system, I do not know how it resets.

If anyone knows any good methods for resetting a combo that is not a module(I kind of want to figure it out without the use of modules), please let me know.

If needed, I can post my current sword combo script.

Thanks.

You can use tick()

So basically, all it does is return how many seconds have passed since 1970.01.01. And you can save this every time the player swings, and then check at the next swing how much time has passed since then.

local swingTimer = 0
local resetTime = 3 -- after how many SECONDS does it reset

--Swing action
if swingTimer < tick() - resetTime then
	--reset combo
else
	swingTimer = tick()
	--slash
end

I’ll try to use it, however I have attempted this in the past and it didn’t work, but I’ll try it out now

After trying to implement this, this is what I got:

This is my code, as of now I have not made them do animations, only update the value.

tool.Activated:Connect(function()
	if swingTime < tick() - resetTime then
		swingTime = tick()
		
		combo.Value += 1
		if combo.Value > 3 then
			combo.Value = 2
		end
		
		print("combo up" .. combo.Value)
	else
		combo = 0
		print("rombo :(")
	end	
end)

Ok, so it appears that I have somehow inversed this.
When I click in the window, it cancels the combo. If I wait for 3 seconds then it continues the combo.

Is combo a NumberValue or a variable? If it is a variable, not an actual Instance inside the workspace, you don’t need to use .Value, just say combo > 3 and combo += 1. The error says that its a variable, not a NumberValue

Sorry, I wrote the code incorrectly. It’s tick() + resetTime

Wait no

Ok no, it’s with -, It’s because I switched the operations. First comes the combo reset, and then the slash. I edited the original code

alright so i did use a + and that fixes the weird reverse thing, however it gets stuck in combo 2 and 3
ill switch back to using -

combo.Value = 0

I tink you forgot to put “value”

Sorry, but even after the code change, the script still doesn’t work.
The output states that the combo keeps getting stuck at the cancel and does not increase the combo at all. However, if I use the original code, the combo system reverses, only increasing combo if I click after 3 seconds rather than the inverse.

Alright, so I just looked up “combo reset tick()” and found the solution.


local swingTime = 0
local resetTime = 0.5

local wasRombo = false

tool.Activated:Connect(function()
	if tick() - swingTime <= resetTime or wasRombo == true then
		if combo.Value >= 3 then
			combo.Value = 1
		end

		combo.Value = combo.Value + 1

		print("combo up" .. combo.Value)
		wasRombo = false
	else
		combo.Value = 0
		print("rombo :(")
		wasRombo = true
	end
	swingTime = tick()
end)

The wasRombo is to check if the previous tick check was a cancel so that you don’t get stuck in rombo limbo

1 Like