GetChildren() doesn't return correct instances

My problem is that my game clones different chunks of map on server-side. And After cloning all of the chunks server calls client event to add some listeners on client side. Heres an example of that:

output(the result of GetChildren() ):
{ED04948C-B702-423F-8C4A-0FCB0A64897C}
workspace(client side):

the problem is getChildren() doesn’t return all instances of parent…
here’s the client code:

		local Component = require(Core.ReplicatedLibraries.Component)
		for _, chunkName in pairs(arrayGeneratedChunkNames) do
			local chunk = workspace.Map.Stages:FindFirstChild(chunkName)
			print(chunk)
			print(chunk.Assets:GetChildren())
			for _, v in pairs(chunk.Assets:GetChildren()) do
				--print(v)
				local ComponentName = v:GetAttribute("Component")
				if not ComponentName then
					continue
				end
				--print(("%* %*"):format(v, ComponentName))
				Component.Apply(v,ComponentName)
			end 
		end

Also the fact if there’s some wait functions in iteration then GetChildren() will be executed as expected to be, now here’s the code how it’d look like with wait functions:

		local Component = require(Core.ReplicatedLibraries.Component)
		for _, chunkName in pairs(arrayGeneratedChunkNames) do
			task.wait(1)
			local chunk = workspace.Map.Stages:FindFirstChild(chunkName)
			print(chunk)
			print(chunk.Assets:GetChildren())
			for _, v in pairs(chunk.Assets:GetChildren()) do
				--print(v)
				local ComponentName = v:GetAttribute("Component")
				if not ComponentName then
					continue
				end
				--print(("%* %*"):format(v, ComponentName))
				Component.Apply(v,ComponentName)
			end 
		end

Do print(chunk.Assets:GetFullName())

I want to make sure you that you’re referencing the proper instances.


{90F7F5EA-0D83-4900-BB3E-EDAE2262E573}

updated code:

		local Component = require(Core.ReplicatedLibraries.Component)
		for _, chunkName in pairs(arrayGeneratedChunkNames) do
			local chunk = workspace.Map.Stages:FindFirstChild(chunkName)
			print(chunk.Assets:GetFullName())
			print(chunk.Assets:GetChildren())
			for _, v in pairs(chunk.Assets:GetChildren()) do
				--print(v)
				local ComponentName = v:GetAttribute("Component")
				if not ComponentName then
					continue
				end
				--print(("%* %*"):format(v, ComponentName))
				Component.Apply(v,ComponentName)
			end 
		end

Try this:

print(#chunk.Assets:GetChildren())

That will tell us how many children are being returned by the function.

Are these parts there when the game runs? Maybe when the code executes the parts are not there yet.

So are the chunks being generated procedurally or in a seperate script?

as i said its generated by server-side script
heres the code:

local MapGeneratorService = require(Core.ServerServices.MapGeneratorService)
			local arrayGeneratedChunkNames = MapGeneratorService.GenerateChunks() -- this function generates map

			for _, chunkName in pairs(arrayGeneratedChunkNames) do
				local chunk = workspace.Map.Stages:FindFirstChild(chunkName)
				
				for _, v in pairs(chunk.Assets:GetChildren()) do
					local ComponentName = v:GetAttribute("Component")
					if not ComponentName then
						continue
					end
					Component.Apply(v,ComponentName)
				end 
			end
			task.wait()
			local ComponentReplicator = require(Core.ReplicatedServices.ComponentReplicatorService)
			ComponentReplicator:Replicate(arrayGeneratedChunkNames) -- call client-side script

So what happens if you add a huge delay(for testing) on the client sided code after the client script is triggered. Im assuming this is an issue with some sort of delay.

1 Like

then it works perfect as i said before in my post

Okay then that means it’s not a fault of get children. What you should do is send a number that represents how many parts should be in a chunk, and then on the client you should wait until the amount of detected parts equals that. Of course, add a max wait time, and if it exceeds that, then send a message to the server for a retry.

1 Like

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