How to wait for a value to change OR wait 20 seconds?

script.Value.Changed:Wait()

how would i make it so the script waits for the value to change, but if the value doesnt change within 20 seconds then just continue anyways

You could try something like:

local testValue = script.Value
local testDebounce = true

for i = 1,20 do
	testValue:GetPropertyChangedSignal("Value"):Connect(function()
		print("yes it has stopped uwu")
		testDebounce = false
		return
	end)
	
	if testDebounce == false then
		break
	end
	
	task.wait(1)
	testValue.Value += 1
	print(tostring(testValue.Value))
end
1 Like

You could try

local Changed = false
local Connection = script.Value.Changed:Connect(function()
    Changed = true
end)

local Waited = 0

repeat
    Waited += task.wait()
until Changed or Waited > 20

Connection:Disconnect()

yeah, that works fine, thanks!

You should be disconnecting this

local Connection
Connection = testValue:GetPropertyChangedSignal("Value"):Connect(function()
	print("yes it has stopped uwu")
	testDebounce = false
	Connection:Disconnect()
	return
end)

Otherwise it could lead to memory leaks.

2 Likes
local Run = game:GetService("RunService")
local Value = script.Value

local Time = tick()
local Fired = false

local Event = Value.Changed:Connect(function()
	Fired = true
end)

while true do
	Run.Heartbeat:Wait()
	if Fired then
		Event:Disconnect()
		print("Event fired!")
		break
	elseif tick() - Time >= 20 then
		print("Event timed out!")
		break
	end
end

Here’s something which provides a little more information (if the event fired or if it timed out).

1 Like