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

Add debounce to renderstepped?

Is there a way to make certain functions in a renderstepped updating function run with a debounce? I'm not quite sure how to do this since im not hugely familiar with renderstepped.

--this functioned controlled by renderstepped, if you dont know what it is then you probs can't help meh

function Update()
	
	AnimationControl()
	ControlStamina()
	
end

--This functions is called by the renderstepped function but i only want this function to run every 5 seconds or so, adding wait(5) would not work since it'd still call the function right? is there a way of debouncing this?

function ControlStamina()
	while isRunning do
		wait(5)
		if Stamina >= 5 then
			Stamina = Stamina - 5
			staminaLevel.Size = UDim2.new(0,(Stamina * 2),0,24) --full frame size is 200 therefore if there is 100 stamina we need to multiply that by 2	
		end
	end
end

--^^^planning to add the stamina replenish part once isRunning is false again, isRunning is controlled accordingly by keyevents

1 answer

✓ Accepted answer

Renderstepped just calls a function up to 60x a second (thus making it smoother than 'wait' for things like animation). We could add debounce to your ControlStamina function, but I think you'd do better if you just decreased the stamina bar continuously, like so:

local drainRate = 1 --stamina points lost per second of running
function ControlStamina(deltaTime)
	if isRunning then
		Stamina = math.max(0, Stamina - deltaTime * drainRate)
		staminaLevel.Size = UDim2.new(0,(Stamina * 2),0,24) --full frame size is 200 therefore if there is 100 stamina we need to multiply that by 2   
	end
end		

For this script, you'd have to send how much time has passed to the function. This is provided to functions that listen to the RenderStepped event (it's the "step" argument).

If you really wanted to use debounce, it would look like this:

controlStaminaDebounce = false
function ControlStamina()
	if controlStaminaDebounce then return end
	controlStaminaDebounce = true
	--rest of function here (while isRunning do loop)
	controlStaminaDebounce = false
end

1 comment

0

Thanks so much, exactly what i wanted, i didnt want to have to use something other than renderstepped to run my functions

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