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

How do i check the time of how long something is set true or false?

Asked by
Zunaxo -3
6 years ago

How do i check how long something is there like lets say how long a boolvalue is set true and if its set true for like 10 seconds it prints something.

1 answer

Log in to vote
2
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

You can use tick() to get the difference between the times

At the top of the script, you can do this:

local startTime

Then when you set the value to true, you can do this:

bool.Value = true
startTime = tick()

And then when you set it to false, you can check if the difference between the new tick and the start time is more than 10

bool.Value = false
local timeTaken = tick() - startTime
if timeTaken >= 10 then
    print("10 seconds later")
end

If you just wanted to do all this whenever the value is changed, it would look something like this:

local bool = --set to boolvalue
local startTime
bool.Changed:Connect(function(newVal)
    if newVal == true then
        startTime = tick()
    else
        local timeTaken = tick() - startTime
        if timeTaken >= 10 then
            print("More than 10 seconds")
        end
    end
end)
0
Thank you! Zunaxo -3 — 6y
Ad

Answer this question