Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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?

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. xXTouchOfFrostXx 125 — 5y

2 answers

Log in to vote
0
Answered by
karlo_tr10 1233 Moderation Voter
5 years ago

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


0
What does that while wait() loop do? Is it necessary? SRdeagle 7 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago
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


0
If you are going to just paste my code and add something give dome sort of credit. karlo_tr10 1233 — 5y
0
He asked about way to do it he didn't request the whole script karlo_tr10 1233 — 5y
0
Also this code wouldn't work as on value won't update if it loop is not inside the other loop karlo_tr10 1233 — 5y

Answer this question