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

How could someone count for how long the tool was activated?

Hello, so I am trying to make something in a tool that tells you for how long the left mouse button was pressed. The only idea I could come up with was to make a loop that activates on the Mouse.Button1Down event and counts in 0.1 seconds. Then I would break that loop and just use the information that I got in the Mouse.Button1Up part. However I can't break the loop. So, any better ways of doing this or just a way to break the loop when the left button goes up?

1 comment

0

You could make a variable which is false by default, if mouse down variable goes true. Then you could use a loop which checks if the variable is true or false. If variable is true, count ticks. if it's false, stop loop.

2 answers

✓ Accepted answer

xXTouchOfFrostXx answered your question.

local on = false
script.Parent.MouseButton1Down:Connect(function()
	on = true
end)

script.Parent.MouseButton1Up:Connect(function()
	on = false
end)

while wait() do
	while on == true do
		-- Count time
	end
end


1 comment

0

What does that while wait() loop do? Is it necessary?

local timer = 0
local on = false
script.Parent.MouseButton1Down:Connect(function()
    on = true
end)

script.Parent.MouseButton1Up:Connect(function()
    on = false
end)

while on do--works while on is true
    wait(0.1)--waits the amount of time
timer = timer + 0.1--adds on the wait value onto the timer
end


3 comments

0

If you are going to just paste my code and add something give dome sort of credit.

0

He asked about way to do it he didn't request the whole script

0

Also this code wouldn't work as on value won't update if it loop is not inside the other loop

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