Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to figure out the time between each click?

So I want to make a script that finds out the time in between clicks.

For example:

Player clicks

Player clicks again

Prints the time before he clicked the second time

I really need help! So thank you in advance.

1 comment

0

Use :tick()

3 answers

✓ Accepted answer

Ok here you go, this should print the time difference between each click (currenttime - lasttime = difference).

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local lasttime = tick()
mouse.Button1Down:Connect(function()
	local t0 = tick()
	local timediff = t0 - lasttime -- pro maths here

	print("Last click was " .. timediff .. " seconds ago!")
	
	lasttime = t0 -- The other answers didn't update the last time :(
end)

(an upvote would be appreciated)

1 comment

0

Thank you so much it works!

Add a Debounce.

Local db = false

If not db then
db = true
--Do something
wait (1) -- How long the duration of the click again
db = true

1 comment

0

This isn’t answering the question.... don’t read as fast next time

Pretend this Script is a child of a ClickDetector, and you're changing a part color.

local StartTime = tick()
local ClickDetector = script.Parent

ClickDetector.MouseClick:Connect(function()
	workspace.Part.BrickColor = BrickColor.new("Fog") -- example Part
	print("This took: "..StartTime - tick()) -- you can use math.abs() to get the absolute value i think, not sure dont judge me :(
end)

Please accept this answer if this helped!

3 comments

0

tick() just keeps going up.

0

well yea, you would have to use some math.

0

(its tick() - StartTime) remember to do StartTime = tick() at the end of the function (after print())

This discussion is preserved as it was. New answers, comments, and votes are closed.