How to make task.spawn() return to the beginning when called 2 times or more. (Resent)

I want to make a combat system with m1 punch (x4). Which is when player is delaying clicks or wait. The animaton will be reset to the beginning

Script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local Default = require(replicatedstorage.ModuleScript.DefaultSettings)

local runservice = game:GetService("RunService")
local userinputservice = game:GetService("UserInputService")

local mouse = game.Players.LocalPlayer:GetMouse()

local returncombo

local COMBO = 0
local COMBO_DURATION = 0
local RETURNCOMBO_START_COOLDOWN = 2
local LIGHTCOMBO_COOLDOWN = 0.5
local LIGHTCOMBO_LAST_COOLDOWN = 2
local HIT_COOLDOWN = 0.5

local full_combo = false
local hit = false
local hit_debounce = false
local returnHit_debounce = false
local already_hit = false
local holding = false

local debounce = false

local function clickOrHold()
	if debounce then return end

	if not debounce then

		COMBO += 1

		comboHit(COMBO)

		walkSpeed(Default.speed - 4)

		hit = true
		debounce = true
		hit_debounce = false

		task.wait(HIT_COOLDOWN)

		walkSpeed(Default.speed)
		task.wait(COMBO_DURATION)
		debounce = false  
	end
end

local function returnCombo()
	task.wait(RETURNCOMBO_START_COOLDOWN)

	if not full_combo and not hit_debounce then -- I'm still confuse with this one.

		print("returns")
		full_combo = false
		COMBO = 0
	end

	hit = false
	hit_debounce = false
end

local function returnComboThread()

	returncombo = task.spawn(returnCombo)

end


function walkSpeed(speed)
	script.Parent:WaitForChild("Humanoid").WalkSpeed = speed
end


function comboHit(i)
	replicatedstorage.HitboxEvent:FireServer(i)
end


runservice.Heartbeat:Connect(function (deltaTime)

	if COMBO == 0 then already_hit = false end

	if COMBO == 1 or COMBO == 2 or COMBO == 3 then full_combo = false already_hit = true COMBO_DURATION = math.abs(LIGHTCOMBO_COOLDOWN - HIT_COOLDOWN) end

	if COMBO == 4 then COMBO_DURATION = math.abs(LIGHTCOMBO_LAST_COOLDOWN - HIT_COOLDOWN) full_combo = true  task.wait(1) COMBO = 0 end

	if holding then
		clickOrHold()
	end

	if not hit_debounce and hit then
		hit_debounce = true

		returnComboThread()

	end

end)

userinputservice.InputBegan:Connect(function(input,gpe)
	if gpe then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		holding = true
	end

end)

userinputservice.InputEnded:Connect(function (input,gpe)
	if gpe then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		holding = false
	end

end)

The script works fine, but it only returns when there’s a rapid clicks. Not a stalling clicks. Can anyone help me with this?

Ah, I understand. You want to reset a combo when the player waits too long, is that correct?

In this case, I would recommend the use of “tick()”.

In a combo system I developed, I used “tick()” to track the time passed since the last hit.
When a function is called and the hit is activated, create a variable to hold the tick() value.

local timePassed;

local function hit()
    timePassed = tick();
end

Now, to check how long the time has passed since the last hit, we can do:

local timePassed;
local timeThreshold = 1; -- the time it can't exceed

local function hit()
    if (tick() - timePassed) > timeThreshold then
        combo = 0; -- resets combo to the starting combo
        timePassed = tick();
    else
        -- combo code continues as normal
    end
end

This code is meant to be changed and learned by you. I unfortunately cannot modify your code to fit your expectations, but this should help you out.

If you would like to understand tick(), here is my example:

local foo = tick();
task.wait(3);
print(tick() - foo); -- output will be 3, because the script waited 3 seconds since tick() was called

I hope this helped!

2 Likes

I’ll try this solution later. Thank you for the idea of using tick().

1 Like

You can also use a timer module/runservice along with time tracking methods like tick() or deltaTime to reset a combo.

2 Likes

i am the number 1 dthecoolest fan

2 Likes