Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Add debounce to renderstepped?

Asked by 8 years ago

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

Log in to vote
1
Answered by 8 years ago

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
0
Thanks so much, exactly what i wanted, i didnt want to have to use something other than renderstepped to run my functions dragonkeeper467 453 — 8y
Ad

Answer this question