How to wait until an object is completely loaded?

Hello developers, I am currently working on a checkpoint system. I have a folder which holds all the checkpoints and I want to change checkpoints brick colors when the player joins the game. However WaitForChild just waits until the folder is loaded, not the parts so the for loop doesn’t detect any child. Here is my current script:

local checkpoints = workspace:WaitForChild("Checkpoints") -- There are 32 blocks in the folder normally

local function changeColor()
	for index, check in pairs(checkpoints:GetChildren()) do
		check.BrickColor = BrickColor.new("Moss")
		print(check.Name .. " color changed") -- Code doesn't run because there is no children detected.
	end
end

changeColor()

Thanks.

2 Likes

if you know that there are 32 blocks inside the folder then you can just wait until the children count is higher or equal to 32.

local checkpoints = workspace:WaitForChild("Checkpoints") -- There are 32 blocks in the folder normally

repeat task.wait() until (#checkpoints:GetChildren() >= 32)

local function changeColor()
	for index, check in pairs(checkpoints:GetChildren()) do
		check.BrickColor = BrickColor.new("Moss")
		print(check.Name .. " color changed") -- Code doesn't run because there is no children detected.
	end
end

changeColor()
3 Likes

In that case, It might be because you disabled StreamingEnabled, To enable go workspace and in it’s properties enable it.

If it’s enabled and yet not working, You might want to add to the first line: (If it’s a LocalScript only)

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

Here is the folder, I am sure it has 32 elements:

However now the function doesn’t run.

local checkpoints = workspace:WaitForChild("Checkpoints")

repeat task.wait() until (#checkpoints:GetChildren() >= 32)

local function checkColor()
	print("function ran")
	for index, check in pairs(checkpoints:GetChildren()) do
		check.BrickColor = BrickColor.new("Moss")
		print(check.Name .. " color changed") -- Code doesn't run because there is no children detected.
	end
end

checkColor()
2 Likes

You can yield ofc but you can also utilize the ChildAdded event of Instance to detect when a child is added and change the color of that child. Also are you using StreamingEnabled?

1 Like

by just adding
local child = checkpoints:WaitForChild(“7”) at the start it should help

1 Like

I added it at the top of the local script and streaming enabled is set to true but still doesn’t work. NOTE: some of functions are not related to this question look at line 1 and go back to the last function.

if not game:IsLoaded() then game.Loaded:Wait() end
--BELOW NOT RELATED GO TO THE LAST FUNCTION

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PadMovementRemote = ReplicatedStorage:WaitForChild("Movepad")
local CheckpointRemote = ReplicatedStorage:WaitForChild("Check")


local SoundService = game:GetService("SoundService")
local LevelPassSound = SoundService:WaitForChild("LevelPass")


local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(2, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)


local player = script.Parent.Parent
local leaderstats = player:WaitForChild("leaderstats")
local stageStat = leaderstats:WaitForChild("Stage")



PadMovementRemote.OnClientEvent:Connect(function(model)
	local pad = model.pad
	local pos1 = model["1"]
	local pos2 = model["2"]
	

	local moveUp = TweenService:Create(pad, info, {Position = pos1.Position})
	local moveDown = TweenService:Create(pad, info, {Position = pos2.Position})
	

	moveUp:Play()
	wait(2)

	moveDown:Play()
	wait(2)
end)


CheckpointRemote.OnClientEvent:Connect(function(checkpoint)
	if checkpoint.ClassName == "Part" then 
		checkpoint.BrickColor = BrickColor.new("Moss")
		if not LevelPassSound.Playing then
			LevelPassSound:Play()
		end 
	end
end)

--BELOW RELATED 

local checkpoints = workspace:WaitForChild("Checkpoints")

repeat task.wait() until (#checkpoints:GetChildren() >= 32)

local function checkColor()
	print("function ran")
	for index, check in pairs(checkpoints:GetChildren()) do
		check.BrickColor = BrickColor.new("Moss")
		print(check.Name .. " color changed") -- Code doesn't run because there is no children detected.
	end
end

checkColor()

2 Likes

The parts are prolly not loaded in when the script runs. To combat the issue, parent the blocks under a model and set the model’s ModelStreamingMode to Atomic or Persistent. After that just wait for the model to load which will only happen if all the children have been loaded in.

3 Likes

It works! Thanks for the reply.

Also is there a way to do that with a folder? (like that property)

Well, folder means headache cause the parts will stream in and out and you would have to use the ChildAdded event I mentioned before or use a custom listener.

1 Like

Yeah it isn’t necessary, thank you again!

1 Like

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