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

How to detect if value hasn't changed for certain time?

Asked by 4 years ago

I'm making an military game, mostly including NPCs. Every NPC has a binded bool value called "shooting", that sets on "true" when NPC is shooting, and turns "false" when NPC stops shooting.

My goal is to detect if NPC isn't shooting for, let's say, 5 seconds. Script can detect that by reading "shooting" value, but I don't know how would I detect that it doesn't change. That's what I achieved so far, but it doesn't really work.

local debouce = false
local value, changed = nil, false
script.Value.Changed:Connect(function(newValue)
    changed = true
    value = newValue
    wait(5)
    if newValue == value and changed == false then
        print("Not changing!")
    end
    changed = false
end)

Thanks in advance!

Krzysiek

0
hmm let me think my son... kujekmarek 24 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Nevermind, I've figured it out by myself :)

For those who are wondering how I've achieved this:

local value, flag = nil, 0
script.Value.Changed:Connect(function(newValue)
    flag = flag + 1
    if newValue ~= false then return end

    local localFlag = flag
    value = newValue

    wait(5)

    if newValue == value and flag == localFlag then
        print("Not changing!")
    end
end)
0
If this answers your question, mark this as answered! Glad to see that you figured it out! :) Erie20 102 — 4y
0
I tried to but I guess I can't mark my own answer KrzysiekRoblox 50 — 4y
Ad

Answer this question