Table printing an empty table

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)
1 Like

Is this a local or server script?

1 Like

hey killer,
thanks for your resposne mate; its local

Put this line and it should work

if not game:IsLoaded() then
    game.Loaded:Wait()
end
1 Like

funny enough when i try that it throws me an error because the script ran before the stuff has even loaded.
image

i tried contentprovider:preloadasync() as well but that didn’t work.

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

i really appreciate the help tho, cheers mate!

1 Like

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.

2 Likes

what the difference between ganeloaded and player.CharacterAdded:Wait()?

1 Like

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.

2 Likes

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
1 Like

game.loaded fires when the client loads everything for the first time while CharacterAdded fires everytime a players character spawns

3 Likes

hey oni,
thanks a million for your help and links mate. i got it; simple solution but does the job. cheers!!

indeed you are right byte. it somehow worked idk why but anyways. :man_shrugging:

thanks very much for the resources mate; i had a read over it earlier :slight_smile:

i honestly really like the second way that you described. i’ll defo use that in the future. thanks very much for your help mate!!! :slight_smile:

1 Like

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
1 Like

ah, dooly noted. thanks very much for the info oni, i appreciate you taking the time to explain and for helping other people out as well as myself. :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.