Hi, I’m having this problem. In workspace there is a folder called Folder which the server constantly adds parts (and removes them over time). The client must process each of these parts (not forgetting any or processing the same part twice). To do this, create the following code on the client side.
local OnPartAdded = function (part)
-- some code
end
for _, part in ipairs(workspace.Folder:GetChildren()) do
OnPartAdded(part)
end
workspace.Folder.ChildAdded:Connect(OnPartAdded)
In Studio it seems to work well, but in production (where the unthinkable happens) I make these two assumptions.
Assumption 1: It is possible that while the loop is running a part is added and not captured by ChildAdded and therefore will not be processed.
So I changed it as follows.
workspace.Folder.ChildAdded:Connect(OnPartAdded)
for _, part in ipairs(workspace.Folder:GetChildren()) do
OnPartAdded(part)
end
Assumption 2: But a moment before doing GetChildren the server can add a part which will be captured by ChildAdded and will also be processed in the loop.
The question is, can these assumptions happen or is it impossible for them to happen? If so, how can one solve these problems?
I guess I don’t understand why you have a loop at all. Why not just
workspace.Folder.ChildAdded:Connect(OnPartAdded)
?
Also, as a side note, are you sure you want the client to be the one responsible for processing these parts? Why not have the server process them instead, and just trigger a RemoteEvent on the client with whatever data they need. No need for a folder or anything!
Having the client responsible for this, especially if it’s critical stuff like you make it sound, could open you up to some tricky exploits.
One solution is to tag the parts that have been processed. Either by inserting a stringValue into the object and checking for it, or putting the object into a CheckedObjects table and checking for it in there.
The loop is because, when the user joins the server the workspace.Folder may have parts (which will not be captured by ChildAdded). Localscript can be replicated after some parts have been replicated.
The processing is mainly FX (textures, short animations, lights). Non-critical stuff.
I’m mainly curious about how roblox behaves in this case.
This sounds interesting and effective. Simple solutions are the best. Although I’m still wondering if a solution needs to be applied. Thank you for your answer.
Your first assumption is very unlikely, but possible if your OnPartAdded is yielding or the server is extremely rapid at making new parts. Your second assumption will never happen, because the total run time between line 1 and 2 is effectively zero. There is no “moment” for a child to be added and not be caught in the GetChildren call.