How To Check The Amount Of Time Passed Before A Function Is Run, And Make The Script Continue If Not

Basically, what I am doing is I want to check if a button is pressed, but if ten seconds go by, I want the script to move on. (And disconnecting the listening for the MouseButton1Up event) Is there a way to do this?

1 Like

You can pretty much do something like this.

local event = mousebuttonEvent:Connect(handleMouseClick)

task.wait(10)
event:Disconnect()
--continue on down script here
1 Like

That doesn’t acclimate for if the player actually does click the button, then they would be waiting unnecessary time.

Ahh yeah. I don’t have enough time to give a detailed answer, but the easiest way is to make the code to run after be in a function, then run the function at the end of the mouse clicked or at the end of the delay. I can give a more detailed answer in a few hours if necessary, but my time is up for now.

What I did is made a while loop and added 0.1 to a value every time, and if the value was equal to ten then i broke the connection and the loop

2 Likes

dunno if this has been solved or not but you can use time() to get the starting and ending times and find the difference of them to know if 10 seconds has passed.
ex:

local startTime = time()
local event = mousebuttonEvent:Connect(function()
    if time() - startTime >= 10 then 
        event:Disconnect()
        return
    end

    --do things
end)

and if you dont want the listenner to stay open after the 10 seconds, you can use task.spawn to create a seperate thread that closes it without interrupting your code.
ex

task.spawn(function()
    task.wait(10)
    event:Disconnect()
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.