How to check if a player clicked with their mouse twice?

I want to create my own combat system and I have animations and stuff set however I’m trying to figure out how to check if the player clicked once and then clicked again. This might sound vague so here’s an example. Let’s say a player clicked once, then they would punch with their left hand, now let’s say they clicked again. Now they would punch with their right hand. However, you can’t do this if you put this all in one Button1Down event, so would you have to use another Button1Down event separately or something?
Here’s another example in code(It would be in starter player scripts):

local localPlayer = game.Players.LocalPlayer -- get the localPlayer
local mouse = localPlayer:GetMouse()

mouse.Button1Down:Connect(function()
         --player tapped the first time so play left hand animation
end)

--how would I check that the player clicked again

Now I know that the damage scripts and remote event code isn’t there but the code above is just to give a visual of what I need in code, not what I have. Thanks to all responses in advance!

5 Likes

Create a timer that counts down from whatever number you want and if you press the mouse button again while the time > 0 then do the second action.

1 Like

3 things… how would I create a timer also the whole question was to check if the player pressed the mouse button again… so how would I check that. If you were to click when the timer had more time then 0 wouldn’t you be clicking once and punching with the right and left hand.

1 Like

You could use the tick() function for this. It returns the time in seconds that has passed since 1st of january 1970.

local Time = tick()

mouse.Button1Down:connect(function()
        if tick() - Time < 0.5 then -- tick() - Time would be the time elapsed
                print("double Click!")
        end
        Time = tick()
end)
1 Like
local localPlayer = game.Players.LocalPlayer -- get the localPlayer
local mouse = localPlayer:GetMouse()
local Clicks = 0
local timeA = tick()
mouse.Button1Down:Connect(function()
timeA = tick()
if Clicks ~= 2 then
Clicks = Clicks+1
--Put whatever you want the script to do
else -- If Clicks == 2
Clicks = 0
end
if tick() > - timeA < 1 then -- If player doesn't click again in the next second
Clicks = 0
end
if Clicks == 2 then 
Clicks = 0
end
end)
1 Like

Wouldn’t tick() - Time always result in 0 since they’re both the same thing.

2 Likes

This seems like how I would combine attacks to do combos but how would it figure out if the player clicked again. I think you misunderstood me when I said click with their mouse twice. I meant that the first time you click you do something and the second time you do something you do something else.

1 Like
local localPlayer = game.Players.LocalPlayer -- get the localPlayer
local mouse = localPlayer:GetMouse()
local Clicks = 0
local timeA = tick()
mouse.Button1Down:Connect(function()
timeA = tick()
Clicks = Clicks+1
if Clicks == 1 then
--First click
end
if tick() - timeA < 1 then -- If player doesn't click again in the next second
Clicks = 0
end
if Clicks == 2 then
-- Second click 
Clicks = 0
end
end)

I think this is what you meant then?

Edit: Error fix

3 Likes

Try this:

local last

mouse.Button1Down:Connect(function()
    local now = tick()
     
    if not last or (now - last) > 2 then
        last = tick()
    elseif (now - last) <= 2 then
         last = nil
         print("do stuff")
    end
end)
1 Like

No. tick() works a little like this:

local timeElapsed = tick() --Lets say this returns 120seconds 
wait(2)
print(timeElapsed) -- This variable currently stores 120, so it will print 120

timeElapsed = tick() -- This will now equal 122
print(timeElapsed) -- This will now print 122.

wait(5)
print(tick()-timeElapsed) -- This will print tick()--Which is now 127 minus 122, hence 5 seconds.

EDIT: Format

1 Like

Is there a way to do this without checking the time because I just want every first click to be one thing and every second click to be another thing.

1 Like
local localPlayer = game.Players.LocalPlayer -- get the localPlayer
local mouse = localPlayer:GetMouse()
local Clicks = 0

mouse.Button1Down:Connect(function()
Clicks = Clicks+1
if Clicks == 1 then
--First click
end

if Clicks >= 2 then
-- Second click 
Clicks = 0
end
end)

Then this should be it.

14 Likes

Maybe if you could create a timer, That whenever you click it will count how many times somebody clicked the screen OR tapped the screen (just giving some ideas).

2 Likes

dose this work in mobile i haven’t tested it yet if it dose can you give me the code