Help with a combo system

I’m currently trying to create a combo-counter logic script that detects when you m1, and uses a combo attribute to detect how many m1s you’ve done. I set the m1 limit to 4, but when I test this in game, the game doesn’t know that when the combo counter turns to 4, it should reset. Instead, the combo counter resets when you m1 when the combo counter is 4., making 5 m1s.

I’m using the WCS combat framework for this

This is the part of the script I’m having trouble with.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WCS = require(ReplicatedStorage.WCS)
local Debris = game:GetService('Debris')

local M1 = WCS.RegisterSkill("M1")

local MaxCombo = 4
local lastSwing = {}
local ComboCooldown = 1
local FinalHitCooldown = 2

function M1:OnStartServer()
	local char = self.Character.Instance
	if not char then return end
	
	self.Character.Humanoid.WalkSpeed = 7
	self.Character.Humanoid.JumpPower = 0
	
	if not char:GetAttribute("Combo") then
		char:SetAttribute("Combo", 1)
	end
	
	--Combo Logic
local combo = char:GetAttribute("Combo")
	
	-- Combo Counter Logic
	if lastSwing[char] then
		local passedTime = tick() - lastSwing[char]
		if passedTime <= ComboCooldown then
			if combo >= MaxCombo then
				print("Combo = 4")
				self:ApplyCooldown(FinalHitCooldown)  -- Longer cooldown after completing the combo 
				self:PlayAnimation(char:GetAttribute("Combo"))
				char:SetAttribute("Combo", 4)
				task.wait(.8)
				self.Character.Humanoid.WalkSpeed = 16
				task.wait(.2)
				self.Character.Humanoid.JumpPower = 50
			else
				print("Combo increased to " .. (combo + 1))
				char:SetAttribute("Combo", combo + 1)
				self:ApplyCooldown(0.45)  -- Regular cooldown between hits
				self:PlayAnimation(char:GetAttribute("Combo"))
				task.wait(.4)
				self.Character.Humanoid.WalkSpeed = 16
				task.wait(.2)
				self.Character.Humanoid.JumpPower = 50
			end
		else
			print("Combo = 1")
			char:SetAttribute("Combo", 1)
			self:ApplyCooldown(0.45)  -- Regular cooldown if combo resets
			self:PlayAnimation(char:GetAttribute("Combo"))
			task.wait(.4)
			self.Character.Humanoid.WalkSpeed = 16
			task.wait(.2)
			self.Character.Humanoid.JumpPower = 50
		end
	else
		print("Combo = 1")
		char:SetAttribute("Combo", 1)
		self:ApplyCooldown(0.4)  -- Regular cooldown for the first hit
		self:PlayAnimation(char:GetAttribute("Combo"))
		task.wait(.45)
		self.Character.Humanoid.WalkSpeed = 16
		task.wait(.2)
		self.Character.Humanoid.JumpPower = 50
	end
	
	lastSwing[char] = tick()
	
end

Here’s a video further explaining what I mean.

The 4th m1 repeats itself.
I’ve tried adding another if statement checking if the combo counter is 4 where the combo counter increases, but that didn’t help.

1 Like

its because the combo logic doesnt reset the combo counter when it hits the max value, but instead waits for the next M1 to trigger.

Heres the corrected code:

local MaxCombo = 4
local lastSwing = {}
local ComboCooldown = 1
local FinalHitCooldown = 2

function M1:OnStartServer()
	local char = self.Character.Instance
	if not char then return end
	
	self.Character.Humanoid.WalkSpeed = 7
	self.Character.Humanoid.JumpPower = 0
	
	if not char:GetAttribute("Combo") then
		char:SetAttribute("Combo", 1)
	end
	
	local combo = char:GetAttribute("Combo")
	
	if lastSwing[char] then
		local passedTime = tick() - lastSwing[char]
		if passedTime <= ComboCooldown then
			if combo >= MaxCombo then
				print("Combo = 4")
				self:ApplyCooldown(FinalHitCooldown)
				self:PlayAnimation(char:GetAttribute("Combo"))
				
				task.wait(.8)
				char:SetAttribute("Combo", 1)
				
				self.Character.Humanoid.WalkSpeed = 16
				self.Character.Humanoid.JumpPower = 50
			else
				print("Combo increased to " .. (combo + 1))
				char:SetAttribute("Combo", combo + 1)
				self:ApplyCooldown(0.45)
				self:PlayAnimation(char:GetAttribute("Combo"))
				
				task.wait(.4)
				self.Character.Humanoid.WalkSpeed = 16
				self.Character.Humanoid.JumpPower = 50
			end
		else
			print("Combo reset to 1")
			char:SetAttribute("Combo", 1)
			self:ApplyCooldown(0.45)
			self:PlayAnimation(char:GetAttribute("Combo"))
			
			task.wait(.4)
			self.Character.Humanoid.WalkSpeed = 16
			self.Character.Humanoid.JumpPower = 50
		end
	else
		print("Combo = 1")
		char:SetAttribute("Combo", 1)
		self:ApplyCooldown(0.4)
		self:PlayAnimation(char:GetAttribute("Combo"))
		
		task.wait(.45)
		self.Character.Humanoid.WalkSpeed = 16
		self.Character.Humanoid.JumpPower = 50
	end
	
	lastSwing[char] = tick()
end

I’ve already tried something like this but it didn’t work, but I discovered that if I set the max combo to one less than what I want it to be (4 m1s → MaxCombo of 3 ) then it works fine.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.