Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

0

hmm let me think my son...

1 answer

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)

2 comments

0

If this answers your question, mark this as answered! Glad to see that you figured it out! :)

0

I tried to but I guess I can't mark my own answer

This discussion is preserved as it was. New answers, comments, and votes are closed.