What do you want to achieve? Keep it simple and clear!
I want to play an image sequence. I use delta time to ensure that the image sequence plays at the same time for every user no matter what fps they are running on.
What is the issue? Include screenshots / videos if possible!
I want the script to yield until it is done playing the image sequence. Because I have to make a connection to renderstepped in order to get the delta time and play it every frame it opens a new thread and doesn’t yield
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I did try searching about this topic on the devforum but what I came across just seemed way too overcomplicated
Here is the relevant piece of code
local Connection
local FrameTime = 0
Connection = game:GetService("RunService").RenderStepped:Connect(function(DT)
local NewDT = DT * FPS
FrameTime += NewDT
local NeededFrame = script.ImageSequences[ImageSequence]:FindFirstChild(tonumber(math.ceil(FrameTime)))
if NeededFrame then
Image[Property] = NeededFrame.Texture
else
print("Disconnect")
Connection:Disconnect()
end
end)
You could try making another loop that yields until a value set by the renderstep disconnects itself
local Connection
local FrameTime = 0
local EndYielding = false
Connection = game:GetService("RunService").RenderStepped:Connect(function(DT)
local NewDT = DT * FPS
FrameTime += NewDT
local NeededFrame = script.ImageSequences[ImageSequence]:FindFirstChild(tonumber(math.ceil(FrameTime)))
if NeededFrame then
Image[Property] = NeededFrame.Texture
else
print("Disconnect")
EndYielding = true
Connection:Disconnect()
end
end)
repeat
task.wait()
until EndYielding
--continue other code below
I was thinking of doing this but wanted to look for a solution without using wait() or task.wait() since it should be avoided if possible. I really appreciate the answer and I will use this for now.
Here’s an implementation that doesn’t rely on busy waiting/polling (wait, task.wait).
local Game = game
local RunService = Game:GetService("RunService")
local BindableEvent = Instance.new("BindableEvent")
local Connection --'Connection' upvalue.
local function OnRenderStep(DeltaTime)
--Do code.
if true then --'true' in this case represents a 'breaking' condition.
Connection:Disconnect() --Disconnect the connection.
BindableEvent:Fire() --Fire the BindableEvent.
end
end
Connection = RunService.RenderStepped:Connect(OnRenderStep) --Create the connection.
BindableEvent.Event:Wait() --Wait for the BindableEvent to be fired.
--Execute rest of code.
Just to note, event connections will always create alternate threads when fired (this behavior is unavoidable).