Combo Script. Need help ASAP

Help I tried making a combo system. But I don’t know why it starts with 2 on the first click, and it won’t properly reset the combo variable. :sob: :sob: :sob: :sob: Maybe I’m just ass at coding

local ComboV = 0
local maxCombo = 4

local lastM1Tick = 0
local resetTime = 1

local RS = game:GetService("RunService")

local function combo()
    local Tick = RS.Stepped:Wait()
    
    if Tick - lastM1Tick < resetTime then
        if ComboV < maxCombo then
            ComboV += 1
            print(ComboV)
        else
            ComboV = 1
            print(ComboV)
        end
    else
        ComboV = 1
    end
    lastM1Tick = Tick
end

tool.Activated:Connect(function()
    combo()
end)

add a debounce function with this code:

debounce = false

tool.Activated:Connect(function()
    combo()
    debounce = true
    if debounce = true then
        return end
    task.wait(5)
end)

i didn’t test this, let me know if it worked
also
debounce = false
goes at the first line, while

tool.Activated:Connect(function()
    combo()
    debounce = true
    if debounce = true then
        return end
    task.wait(5)
end)

goes at the end

When the event fires the combo function will run regardless of the debounce and also there’s nothing to reset the debounce. It should be like this:

debounce = false

tool.Activated:Connect(function()
    if debounce = true then return end
    debounce = true

    combo()
    
    task.wait(5)
    debounce =  false
end)

Edit: This is not the solution I will post that next

your lastM1Tick is set to 0, this means tick-lastm1tick is beyond the reset time, and so when you click for the first time it resets your combo and doesn’t print the 1, it still works though. add a print statement when you reset the combo and you will see

Cz im on mobile it took time to re-arrange stuff but here:

local Debounce = false

local ComboV = 0
local maxCombo = 4

local resetTime = 1

tool.Activated:Connect(function()
	if Debounce then return end
	Debounce = true
	
	if ComboV == maxCombo then ComboV = 0 end
	ComboV += 1
	
	print("Current combo: ", ComboV)
	
	task.wait(resetTime)
	Debounce = false
end)

I rewrote it and added comments, I suggest writing things like this in the future if you dont want to use OOP, if you have any questions on why I did it like this just let me know

local RS = game:GetService("RunService")



local MAX_COMBO = 4
local M1_COOLDOWN = 0.1
local M1_RESET_TIME = 1

local lastM1Time = 0
local comboCount = 0

local function Combo()
	local currentTime = RS.Stepped:Wait()
	
	local timeElapsed = currentTime - lastM1Time
	
	
	if timeElapsed < M1_COOLDOWN then -- if they try to punch before the cooldown is up do nothing
		return
	end
	lastM1Time = currentTime
	
	if timeElapsed > M1_RESET_TIME then -- if its been long enough since the last punch, reset the combo
		comboCount = 0
	end
	
	
	
	comboCount = (comboCount % MAX_COMBO) + 1 --increases the combo count by 1 and also makes sure it doesnt go over the max combo
	
	print(comboCount)
	
	
end





tool.Activated:Connect(function()
	Combo()
end)
1 Like

If the next tick takes 30 seconds, it won’t do anything since it’s more than the reset time. So you should change the < to be a >

I don’t think debounce would make any difference since op is already doing the tick refresh method. Unless the function takes longer than the ResetTime I don’t think this is necessary.

Hey sorry for the late reply but is this good with the server? I made the faulty code in the server. I’m using attributes now btw

Also thanks for helping me, I’ll try making similar code in the future

Okay thanks for your input @BlueBloxBlast!

1 Like