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()
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()
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()
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?
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()
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.
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.