Tick() is a good way to go about this and usually the thing I default to for this kind of mechanic. Basically we can use it to compare the change in time when it crosses some time interval.
All we need is to make a timer to let us know that we can do something after 'X' seconds:
local RunService = game:GetService("RunService")
local timeInterval = 1.5
local startTime = tick()
while true do
local currTime = tick()
local deltaTime = currTime - startTime
if deltaTime > timeInterval then
print("Stop!")
break
end
RunService.Heartbeat:Wait()
end
I think the main issue you're running into is not with the above explanation but more of how to implement this using mouse input. Fortunately, we can use ContextActionService to bind our mouse inputs.
We can use the :BindAction function of this service to bind simple input for our left mouse button. Taking a look at its documentation, it tells us that it provides three useful parameters whenever the bounded action does happen. This is the action name that we gave it, the state of that input, and the information of the input object.
We need the state of the action to see whether the input had just began or has just ended. We can make a sort of switch to help us get what we want.
local ContextActionService = game:GetService("ContextActionService")
local function MouseInput(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("Mouse left was pushed!")
elseif inputState == Enum.UserInputState.End then
print("Mouse left was released!")
end
end
ContextActionService:BindAction("mouseInput", MouseInput, false, Enum.UserInputType.MouseButton1)
The rest is as simple as bringing in the loop we had made earlier. In my case, I just want to call a function that will do that for me to keep things readable.
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local timeInterval = 1.5
local isLeftButtonDown = false
local function onMouseHeld()
local startTime = tick()
while isLeftButtonDown do
local currTime = tick()
local deltaTime = currTime - startTime
if deltaTime > timeInterval then
print("Mouse was held long enough")
break
end
RunService.Heartbeat:Wait()
end
end
local function MouseInput(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
isLeftButtonDown = true
onMouseHeld()
elseif inputState == Enum.UserInputState.End then
isLeftButtonDown = false
end
end
ContextActionService:BindAction("mouseInput", MouseInput, false, Enum.UserInputType.MouseButton1)
The only thing to explain is the use of a global variable. We use this for the condition in the while loop to make sure the player doesn't let go before those 1.5 seconds.
And that should be the end of it. Hopefully what I've written is what you wanted since it's what I understood. Inside of line 17 is where you'd want to run your own code whenever the player holds their left mouse button for 'x' seconds.
####Let me know if you need something explained or if I had missed anything
2 comments
No, that is simply just clicking I am talking about when you hold down your mouse button longer than a simple click. It will activate regardless if you held the mouse button or not.
That's the only way I know of unfortunately.