From 1f38b462556c994f8ac99f8a1887ad63bbeb4d46 Mon Sep 17 00:00:00 2001 From: hoontee Date: Fri, 15 Nov 2024 22:41:17 -0600 Subject: [PATCH] Fix ClientInstanceStream race condition --- Pronghorn/New.luau | 32 +++++++++++++++++--------------- Pronghorn/init.luau | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Pronghorn/New.luau b/Pronghorn/New.luau index b3d6d4b..17e2fea 100644 --- a/Pronghorn/New.luau +++ b/Pronghorn/New.luau @@ -473,7 +473,18 @@ function New.ClientInstanceStream(uid: string): (Event<...any>, Event, Inst local finishedEvent: Event<...Instance> = New.QueuedEvent("InstanceStream Finished Event") local streamEvent: Event = New.QueuedEvent("InstanceStream Stream Event") - container.Destroying:Once(function() + local function checkFinished(): boolean + if #instances == numInstances then + finishedEvent:Fire(table.unpack(instances)) + finishedEvent:DisconnectAll() + streamEvent:DisconnectAll() + container:FireServer() + return true + end + return false + end + + container.Destroying:Once(function(): () finishedEvent:DisconnectAll() streamEvent:DisconnectAll() end) @@ -486,30 +497,21 @@ function New.ClientInstanceStream(uid: string): (Event<...any>, Event, Inst table.insert(instances, child.Value) streamEvent:Fire(child.Value) else - child.Changed:Once(function() + child.Changed:Once(function(): () assert(child.Value) table.insert(instances, child.Value) streamEvent:Fire(child.Value) + checkFinished() end) end end - if #container:GetChildren() == numInstances then - finishedEvent:Fire(table.unpack(instances)) - finishedEvent:DisconnectAll() - streamEvent:DisconnectAll() - container:FireServer() - else - container.ChildAdded:Connect(function(child: Instance) + if not checkFinished() then + container.ChildAdded:Connect(function(child: Instance): () assert(child:IsA("ObjectValue") and (child.Value or child.Changed:Wait() and child.Value)) table.insert(instances, child.Value) streamEvent:Fire(child) - if #container:GetChildren() == numInstances then - finishedEvent:Fire(table.unpack(instances)) - finishedEvent:DisconnectAll() - streamEvent:DisconnectAll() - container:FireServer() - end + checkFinished() end) end diff --git a/Pronghorn/init.luau b/Pronghorn/init.luau index d16258e..bfd3bde 100644 --- a/Pronghorn/init.luau +++ b/Pronghorn/init.luau @@ -31,7 +31,7 @@ ║ ██████▀██▓▌▀▌ ▄ ▄▓▌▐▓█▌ ║ ║ ║ ║ ║ -║ Pronghorn Framework Rev. B77 ║ +║ Pronghorn Framework Rev. B78 ║ ║ https://github.com/Iron-Stag-Games/Pronghorn ║ ║ GNU Lesser General Public License v2.1 ║ ║ ║