Changing a value

Heya,
In my script, I am trying to make a block value change to true, but after x seconds it changes back UNLESS it gets set to true again which would reset the timer of x seconds, if that makes sense.

statusFolder["Stunned"].Value = true
				delay(0.4,function()
					--if tick() - secFromlastAttack >= 0.4 then ## was experimenting here
						statusFolder["Stunned"].Value = false
					--end
				end)

That’s what I currently had.
Any help is appreciated!

Do .stunned not [“stunned”] should work

Um what? I don’t think you’re understanding the problem.

What errors are you getting, or what happens instead of your desired effect?

Instance[ChildName] works, try it yourself

It’s not an error, I’m asking for what kind of approach I should be taking.

A delay function is perfectly fine. It creates a new thread and continues running the rest of the script, and calls your function when the delay is up. If you don’t need the new thread, a wait() works just as fine.

Nono, basically I need some kind of detection system to see if the value was set to true again, hence why I tried to check if the previous attack distance was bigger than the current one, and if so then make the value false but it didn’t seem to work.

BoolValue.Changed:Connect(function(NewValue)
   if NewValue then
      print(true)
   end
end)

will this work :sweat_smile:

local debounce = false

statusFolder["Stunned"].Value = true
secFromlastAttack = tick() --this will reset to latest time every time player get stunned
-- I created this debounce to prevent creating a new thread again,
-- if you have a code below the task.spawn, probably you can put debounce inside task.spawn
if debounce then return end 
debounce = true
task.spawn(function()
	while statusFolder["Stunned"].Value then
		if tick() - secFromlastAttack >= 0.4 then 
			statusFolder["Stunned"].Value = false
			debounce = false
		end
		task.wait(.05) 
	end
end)


Edit : alright made some changes, thanks for feedback

1 Like

YES! Thank you.
I changed some stuff around such as the debounce and wait times etc to make it fit in but the overall method works perfectly.
Thanks alot!

1 Like

use task.wait instead of wait()
also please don’t use tick() for this, try using the os library instead, os.click() could be used

while task.wait() do
	if statusFolder["Stunned"].Value do
		task.wait(0.4)
		stunnedOff()
	end
end

function stunnedOff()
	statusFolder["Stunned"].Value = false
end

Probably as simple as you’ll get it.

This wouldn’t work at all sorry.

Why not? The script looks fine to me, unless you mean it’s not what you’re looking for. Your original post was a little ambiguous.

Yea it’s not what I’m looking for and I’ve already marked this post with a solution. Thanks for trying to help though!