Run a function only when a NumberValue reaches a specific value?

I’m implementing a timer into my game, and I want to run a function when that timer hits 0. I know that a value.Changed:Connect() function or a repeat wait() until function would work, but both can cause unnecessary lag due to how often they are triggered. Are there any other options that are more resource friendly?

9 Likes

i mean can you do

wait(Seconds)
function()

can you give more details or the script its used in?

6 Likes

The thing is, the timer can stop, meaning it doesn’t take a set amount of time to reach 0.

8 Likes

can you show the script that your using this in for more context?

5 Likes

More context isn’t necessary, the only thing in the script is the function that lets the timer start and pause. I don’t see how this would be helpful at all because it is completely unrelated to what I’m trying to do here.

8 Likes

Use a remote event if you want to do something from another script.

5 Likes

okay then,

while wait(1) do
if TimeValue <= 0 do
function()
break
end
end

im not going to format nor make it organized

3 Likes

Again, having a loop to detect a change usually causes lag. Breaking the loop would also prevent it from running again in my case. Starting to think you didn’t read my post…

7 Likes

i did, but you didn’t give any context, any scripts, anything really

i really don’t know what you really want

just use value.Changed atp

value.Changed is the best way to do it, since it only runs when a value is changed

5 Likes

I gave you as much context as I could. I’m making a simple timer and I want a function to run when it hits 0. I can’t wait a set amount of time because the timer can start and stop. That’s literally all that’s going on here. The rest of the script wouldn’t help answer my question, so I’m not going to give it. Don’t know what else you want from me.

And just for the record, when a value is being tweened down, this would run about 100 times a second. Does that sound efficient to you?

4 Likes

That depends on what you’re trying to do, if it’s performance intensive, then yes it may cause lag, but if it isn’t then it won’t

3 Likes

thanks for finally telling me SOME context
yeah it is the best, its better than wait looping…

4 Likes

Just use Value.Changed bro. It’s the easiest way and doesn’t even cause game-breaking lag. There’s no better way imo.

7 Likes

wurd man, thanks for clarifying

i have like 6+ scripts that use value.Changed and it doesn’t lag me at all, i cannot notice it too much
(they also can change in 100ms)

5 Likes

I told you literally all of that in previous posts.

3 Likes

no, i mean what did you want, you told me you had a pause system after you posted

i saw no context man, i was asking for a script… or anything really

4 Likes

I have tried .Changed:Connect() and it ran 100 times every second which, surprise, surprise, made my game lag. If there’s no other options, then you should have just told me. Thanks @demisestruth for the straightforward answer.

This just goes to show how stupid and uninformed some people are on the Devforum, thanks for the laugh guys!

5 Likes

Look man, just use a numbervalue and register when it’s at 0. I’ve worked alongside incredibly experienced scripters and they’ve all used that method. Even if it did slightly lag the game it’d be impossible to notice.

3 Likes

I don’t understand how a numbervalue going down 1 each second would make it fire 100 times?

2 Likes

LOL ok
i will next time i guess
(your the one getting angry after asking something you already had the answer to… maybe look on google for once, or devforums before posting a topic)

2 Likes