Hello. I am currently having a problem scripts. I just can’t get this to work. I tried using variables, while loops, and just anything on my mind.
Code:
mouse.Button1Down:Connect(function()
if active == true then
if cooldown == false then
cooldown = true
local seconds = 0
while mouse.Button1Down do
wait(1)
seconds = seconds + 1
if mouse.Button1Up then
print(tostring(seconds)) -- trying to print seconds here...
break
end
end
wait(waitTime)
player.Character.Humanoid.WalkSpeed = 10
cooldown = false
seconds = 0
end
end
end)
You may be able to use os.time or os.clock or time to measure elapsed time instead of using wait and a seconds count incrementation. Are you using a Gui button or an in-game part button?
I recommend recording the start time as a local variable on Button1Down and recording a finish time on Button1Up. Then just subtract the two for elapsed time.
You can await mouse.Button1Up being fired inside your Mouse1Down event.
mouse.Button1Down:Connect()
local start = tick()
mouse.Button1Up:Wait() --// pause thread until Button1Up is fired
local heldFor = tick() - start --// the current time - the start time = time passed
print(heldFor)
end
Since you’re using a Tool, it’d be the best to use the .Activated event. Here’s the example I made for you:
local Tool = script.Parent -- The Tool.
local TimeStart = nil
Tool.Activated:Connect(function()
TimeStart = os.time() -- This will tell us in UNIX time when you activated the tool.
end)
Tool.Deactivated:Connect(function()
print("You activated the tool for " ..os.difftime(os.time(), TimeStart).. " seconds!") -- os.difftime returns the number of seconds from t1 to t2
TimeStart = nil
end)