✓ Accepted answer
You can use coroutines for this. It works, although for some reason it spams the console with an error no matter what safety checks I implement.
local thread
script.Parent.MouseButton1Down:Connect(function()
thread = coroutine.create(function()
wait(2)
print("been holding for 2 seconds")
end)
coroutine.resume(thread)
end)
script.Parent.MouseButton1Up:Connect(function()
coroutine.close(thread)
end)
Whenever you first press it, it spawns a new thread, which will wait for 2 seconds. In the MouseButton1Up event, we kill that thread, therefore we can be sure that if the thread's still running after 2 seconds, a player was holding it that long.
Some people might recommend you use wait, however, it's probably not a good idea.
Learn more about coroutine
1 comment
But I want to loop 'player.Character:PivotTo(player.Character:GetPivot() * CFrame.new(0, 0, 0.01))' until it is no longer held. Sorry for misunderstanding.