How can I return a value without ending the function?

I have a modulescript with code for moving a switch, and it has a debounce. The problem is that I need it to return the switch position (bool) THEN wait 5 seconds to reset the debounce. Return ends the function there, so how can I do this?

Main script:


_G.Reactor_Coolant_LoopInletValue = false


switch_loopinlet.ClickPart.ClickDetector.MouseClick:Connect(function()
	local res =  inputModule.SwitchMove(switch_loopinlet)
	_G.Reactor_Coolant_LoopInletValue = res
end)

inputModule:

function module.SwitchMove(model)
	local db = model.db
	local val = model.val
	local tweeninfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)
	if db.Value == true then return end
	db.Value = true
	if val.Value == false then
		val.Value = true
		model.Sounds.Sound:Play()
		ts:Create(model.Hinge,tweeninfo,{CFrame = model.OnPos.CFrame}):Play()
	else
		val.Value = false
		model.Sounds.Sound:Play()
		ts:Create(model.Hinge,tweeninfo,{CFrame = model.OffPos.CFrame}):Play()
	end
	--return val.Value
	wait(5)
	db.Value = false
end

Maybe you can just have an event for when the value is changed instead

perhaps task.spawn() a wait that resets the debounce, and then return the value.

task.spawn() makes the code in it asynchronous, as in, everything else wont have to wait for it to finish running

1 Like