so my problem is that my table was printing an empty table because my parts didn’t even load yet in my cutsceneFolder.
when i add a wait before the for loop, it works perfectly but are there any better ways to fix this then adding like a wait in the beginning?
i was trying to yield the script to allow the assets in the folder to load before executing the for loop.
task.wait(1)
local cutScenePartsTable = {}
if cutsceneFolder then
for _, part in ipairs(workspace.cutsceneFolder:GetChildren()) do
if part:IsA("Part") or part:IsA("BasePart") or part:IsA("MeshPart") then
cutScenePartsTable[part.Name] = part
end
end
end
print(cutScenePartsTable)
here’s what worked for me, this worked perfectly honestly and i dont think i could get better than this. i was repeating the wait until one of my parts in my cutscenefolder had loaded in:
local cutsceneFolder = workspace:WaitForChild("cutsceneFolder")
local p1 = cutsceneFolder:WaitForChild("cameraPoint_yellow")
repeat task.wait()
until p1
That is due to Instance Streaming. An easy solution would be to group your cutscene parts in a Model and set it’s ModelStreamingMode to Enum.ModelStreamingMode.Persistent. It will load the unit atomically (completely) and will never stream out - you will still have to wait for the unit itself to load with WaitForChild, but it’s contents will be available as soon as it’s loaded.
This loop doesn’t do anything and is unnecessary. You’re waiting until P1 is no longer nil, that is unneeded because WaitForChild waits until P1 is no longer nil - there is no way P1 can be nil after WaitForChild.
Personally to have the quickest loading time I would just run a for loop waiting for the parts instead of the task.wait.
for i, part in pairs(workspace.cutsceneFolder:GetChildren()) do
workspace.cutsceneFolder:WaitForChild(part.Name)
end
local cutScenePartsTable = {}
if cutsceneFolder then
for _, part in ipairs(workspace.cutsceneFolder:GetChildren()) do
if part:IsA("Part") or part:IsA("BasePart") or part:IsA("MeshPart") then
cutScenePartsTable[part.Name] = part
end
end
end
print(cutScenePartsTable)
Or if you wanted them to load even faster I would use a task.spawn and a repeat wait loop until they’ve all loaded:
local partsToLoad = #workspace.cutsceneFolder:GetChildren()
local partsLoaded = 0
for i, part in pairs(workspace.cutsceneFolder:GetChildren()) do
task.spawn(function()
workspace.cutsceneFolder:WaitForChild(part.Name)
partsToLoad += 1
end)
end
repeat task.wait() until partsLoaded == partsToLoad
-- your code here
Not to be that guy but this solution won’t work at all, ever. Because of the behavior of Instance Streaming, calling GetChildren will return an array of children that exists at that time, so doing this is useless. Additionally, calling task.spawn does not make it “faster,” which is just misinformation.
-- this is wrong. this will probably be 0 by the time this line runs,
-- rendering this method useless.
local partsToLoad = #workspace.cutsceneFolder:GetChildren()
local partsLoaded = 0
-- additionally, looping through GetChildren with this logic will not
-- run any loop anyways, because there are no contents loaded.
for _, part in workspace.cutsceneFolder:GetChildren() do
-- even if some parts are loaded by the time this runs,
-- this still doesn't make any sense because it means
-- this part already exists and does not need to be waited for
workspace.cutsceneFolder:WaitForChild(part.Name)
partsLoaded += 1
end