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!
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.
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)
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)
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.
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)
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)
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.
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)
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).