How do I use tick() to check if M1 was clicked in a certain timeframe?

local t = tick()
wait(2)
print(tick() - t) -- The time elapsed since t was made

While looking for posts to help me implement an M1 combo to my melee weapon, I kept coming across this piece of code. My question is, how do I actually use it?

I have three different attacks, a straight punch, a hook, and a shove. All separate animations. I want to check for time between clicks (tick()), so that if the player hasn’t clicked for a while, the combo resets.
This is how the full combo should look:

2d6b6519ff8fedc35f4a2d13e93366bb

(This is just of me playing the animations back to back.)

I apologize if this question is too broad or anything, I am feeling very tired at the time of writing this. Please let me know if you have anything that can push me into the right direction.

1 Like

I don’t know how your script is setup but you could do this:

local Tool = script.Parent
local Combo = 1

Tool.Activated:Connect(function(hit)
if Combo == 1 then
– Code here
Combo == 2
elseif Combo == 2 then
– Code for combo 2 here
Combo == 3
elseif Combo == 3 then
– Code for combo 3 here
Combo == 1
end)

hoped this helped you should be able to do more combos but for the last combo make sure the combo resets back to 1

I assume you want the animation to be played after a few clicks.
This is how I would approach the code -

local clicks = 2 -- the number of clicks you want 
local checkTime = 1 
local userInputService = game:GetService("UserInputService")

local clicksMade = 0 -- this keeps count of clicks made by you within some time
userInputService.InputBegan:Connect(function(input,gpe)
    if (gpe) then return end 
    if (input.UserInputType == Enum.UserInputType.MouseButton1) then 
        clicksMade += 1
        wait(checkTime) 
        if (clicksMade == clicks) then 
            -- play the animation
            clicksMade = 0 return
        end 
    end 
end)

After you have made a click the click counter starts counting and after the checkTime it checks how many clicks you have made.
Depending on the number of clicks made by the user you can do certain things.
For example depending on " clicks " in my code I have played the animation.

For the best response make the checkTime as less as possible but also keep in mind about if it
will be possible for the user to make multiple clicks by then.

1 Like

I’d like the combo to reset if the player hasn’t clicked again after a while. This is why I’m checking for time between clicks.

o wait a second Ill change it a bit

local Tool = script.Parent
local Combo = 1

Tool.Activated:Connect(function(hit)
if Combo == 1 then
– Code here
Combo == 2
elseif Combo == 2 then
– Code for combo 2 here
Combo == 3
elseif Combo == 3 then
– Code for combo 3 here
Combo == 1
end)

while wait(10) do – change the wait to how long you want it to reset
if Combo == 2 then
Combo = 1
elseif Combo == 3 then
Combo = 1
end)

How I do this is to make a global debounce or something using tick or os.clock, a global number that acts as a combo counter and make another global string called something like Animation

I use RunService to update the debounce and if that debounce is out of range, reset the combo counter, for example

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

local t = 0
local cur = 0
local currentanim = 'Animation1'

UserInputService.InputBegan:Connect(function(i, g)
   if g then return end
   if i.UserInputType == Enum.UserInputType.MouseButton1 then
      t = os.clock()
      cur += 1
      local anim = character.Humanoid.Animator:LoadAnimation(script.Animations[currentAnim]) -- I prefer caching these outside but this is just an example
      if anim then
         anim:Play(fadeTime)
      end
   end
end)

RunService.Stepped:Connect(function()
   local now = os.clock()
   if now-t > 0.5 then -- refresh rate
      cur = 0
   end
   if cur > 5 then -- final anim
      currentanim = 'Animation3'
   elseif cur > 3 then -- second anim
      currentanim = 'Animation2'
   else
      currentanim = 'Animation1'
   end
end)

Edit: Some changes and fixes, just noticed its wrong
Edit2: grammer fixes :<

1 Like

@RVCDev

I’m sure your method works, but I usually refrain from using infinite loops unless I need to. Especially since there will be multiple people in the server with this weapon.

@octanagolamen

I’m not checking for the amount of clicks, rather if whether or not the player clicked again after a set amount of time, which is why I referenced tick().
For example, if the player clicked once, it would prompt the straight punch attack, and if the player clicks again, it would prompt the hook attack. If the player waited longer before the second click, maybe 3 seconds, then it would reset to the straight punch attack.

@ForgetableLife

Like RVCDev, I’d like to refrain from using infinite loops. Plus, I don’t think resetting the combo every 10 seconds would be very effective.

By the way, I am using a tool debounce and am using Tool.Activated. I might convert to using UserInputService, but it just feels easier to use the former.

This is what my provided code was aimed for.
In my preference this would be much simpler than to go about tick or os.time() and more.
Yet if you have a better alternative you can surely go about that.

Here is one for combo resetting( in the post below)

However you will also need another to know if the current animation is still playing before you can print punch again, should be a simple if statement check. ( Not included in the post below)

1 Like