How i can make Combo reset

I can’t understand, how i can reset combo when script running when player press M1
Here code where i try do this:

local Combat = {}
local UIS = game:GetService("UserInputService")

--Параметры
local Combo_Reset_Time = 2
local last_Punch_Time = nil
local currTime = nil
	
function Combat.HitboxCreate(Character)
	local Hitbox = script.Hitbox:Clone()
	Hitbox.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-3)
	Hitbox.Parent = workspace
	return Hitbox
end

function Combat.HitboxDelete(Hitbox)
	wait(.3)
	Hitbox:Destroy()
end

function Combat.Punch(Animations, Plr, PunchCount, LocalPlayer)
	
	local Humanoid = Plr.Character:FindFirstChildOfClass("Humanoid")
	local Hitbox = Combat.HitboxCreate(Plr.Character)
	
	local CurrTime = tick()
	if last_Punch_Time and (currTime - last_Punch_Time > Combo_Reset_Time) then
		PunchCount = 1
		print('Combo Reset')
	end
	last_Punch_Time = currTime
	
	if PunchCount == 4 then
		Humanoid:LoadAnimation(Animations['Punch4']):Play()
	else
		Humanoid:LoadAnimation(Animations['Punch' .. PunchCount]):Play()
	end

	local connect
	connect = Hitbox.Touched:Connect(function(part)
		local hit_player = game.Players:GetPlayerFromCharacter(part.Parent)
		local hit_humanoid = part.Parent:FindFirstChild('Humanoid')
		if not (hit_player and hit_player == LocalPlayer) and hit_humanoid then
			hit_humanoid:TakeDamage(10)
			connect:Disconnect()
		end
	end)
	
	
	Combat.HitboxDelete(Hitbox)
	return PunchCount
end

return Combat
1 Like

Let me think about it briefly. Um… so how about using coroutine?

local resetComboCoroutine = nil

local function ResetComboCountdown()
    local leftTime = 2
    while wait(1) do
        if leftTime == 0 then
            combo = 0
            break
        end
        leftTime -= 1
    end
    resetComboCoroutine = nil
end

So, when player attack something:

if not resetComboCoroutine then
    coroutine.close(resetComboCoroutine)
    resetComboCoroutine = nil
end
resetComboCoroutine = coroutine.create(ResetComboCountdown)
coroutine.resume(resetComboCoroutine)

maybe, it works.

I try do this but not work:

local Combat = {}
local UIS = game:GetService("UserInputService")

--Параметры

local resetComboCoroutine = nil

function Combat.HitboxCreate(Character)
	local Hitbox = script.Hitbox:Clone()
	Hitbox.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-3)
	Hitbox.Parent = workspace
	return Hitbox
end

function Combat.HitboxDelete(Hitbox)
	wait(.3)
	Hitbox:Destroy()
end

function ResetComboCountdown(PunchCount)
    local Combo_Reset_Time = 2
    while Combo_Reset_Time > 0 do
        wait(1)
        Combo_Reset_Time = Combo_Reset_Time - 1
    end
    PunchCount = 0
    return PunchCount
end

function Combat.Punch(Animations, Plr, PunchCount, LocalPlayer)
	
	
	
	local Humanoid = Plr.Character:FindFirstChildOfClass("Humanoid")
	local Hitbox = Combat.HitboxCreate(Plr.Character)
	
	if resetComboCoroutine then
		coroutine.close(resetComboCoroutine)
	end
	
	resetComboCoroutine = coroutine.create(function()
		PunchCount = ResetComboCountdown(PunchCount)
	end)
	coroutine.resume(resetComboCoroutine)
	
	if PunchCount == 4 then
		Humanoid:LoadAnimation(Animations['Punch4']):Play()
	else
		Humanoid:LoadAnimation(Animations['Punch' .. PunchCount]):Play()
	end

	local connect
	connect = Hitbox.Touched:Connect(function(part)
		local hit_player = game.Players:GetPlayerFromCharacter(part.Parent)
		local hit_humanoid = part.Parent:FindFirstChild('Humanoid')
		if not (hit_player and hit_player == LocalPlayer) and hit_humanoid then
			hit_humanoid:TakeDamage(10)
			connect:Disconnect()
		end
	end)
	
	
	Combat.HitboxDelete(Hitbox)
	return PunchCount
end

return Combat

I think it went very wrong… You can now that if you read your code.

Oh, my brain do this

Just need create global variable:
local LastPunchTime = nil

and in function:

local currTime = tick()
if LastPunchTime then
    local elipsed = currTime - LastPunchTime
    if elipsed >= 2 then
        PunchCount = 1
    end
end
LastPunchTime = tick()

Glad it’s working well! congratulation

1 Like

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