How to Access Delta Time outside a Function Call

To make my in-game iterations (things I animate via code, like a rotating coin) adjust with the new FPS settings (thank you Roblox!), I want to move/rotate/fade them in increments per frame, and adjust that value by the delta time (the time between each frame).

I can do this using the Heartbeat, Stepped or RenderStepped functions, as they pass deltaTime (known as step) as a parameter, but this is too complicated for some of my code. Extra functions, handling function connections, etc. Simpler works better.

Is there a delta time global value I can access, a Roblox service that will provide this value, or a built-in function that returns the delta time? I would love to do:

for i = 1, iterations do
    part.Transparency += increment * (deltaTime*60) -- multiplying by 60 means this is the increment rate at 60 FPS. Expect that value to change at other FPS values.
end

Thank you for any help!

1 Like

Are you looking to wait for the frame to pass? You could just do part.Transparency += increment * (task.wait()*60) or part.Transparency += increment * (runServiceEvent:Wait()*60)

If not, you would have to define a variable and update it every time the relevant event fires

1 Like

Close, but I do need something a little different. Delta time is “the time period between [the] last frame and the current [frame].”, according to this game design article.

I had initially thought to use task.wait() for that exact reason, but I learned here it doesn’t actually provide delta time - consider what would happen if you were in the Stepped portion of the frame, then waited for Heartbeat. Since Heartbeat comes right after Stepped, the time measured isn’t the time between frames, but the time between two parts of the same frame. Subtle, but it produces two vastly different effects, which is why I mention it.

I’ll need to check what is returned by the RunService events - I thought :Wait() returned nothing, but this article shows differently. It might be the same as task.wait(), but maybe not. Thanks!

1 Like

You’d have to wrap it in a runservice connection, you could do something like:

local FrameUpdateConnection = nil
local i = 0

FrameUpdateConnection = RunService.RenderStepped:Connect(function(deltaTime)
    i += 1
    if i > iterations then
        FrameUpdateConnection:Disconnect()
        i = 0
        return
    end
    part.Transparency += increment * (deltaTime*60)
end)

Im not sure if this method would be accurate but you could get a deltatime of the last update like so:

local LastUpdate = os.clock()

for i = 1, iterations do
    local now = os.clock()
    local deltaTime = now - LastUpdate
    LastUpdate = now
    part.Transparency += increment * (deltaTime*60) -- multiplying by 60 means this is the increment rate at 60 FPS. Expect that value to change at other FPS values.
end
1 Like

:Wait() will return whatever would have been passed to that connection’s function

Also, task.wait is equivalent to runService.Heartbeat:Wait()

2 Likes