I have local script in playergui that creates buttons for each child of a model in workspace. Sometimes, not all buttons are created because not all the model’s children are seen by the client yet at the start of the game. I can’t use game:IsLoaded() and game.Loaded:Wait() because my local script is in playergui. Using WaitForChild() on the parent model doesn’t wait for the children to load. I’m thinking of making a remote event that ask the server to send back an array of each child, but I feel like that’s not a good workaround. What’s the standard practice when it comes to accessing workspace instances from a playergui localscript when the game first starts (Streaming enabled isn’t even on)?
--local script in playergui
local plots = workspace:WaitForChild("HousePlots")
local plotsToSelect = {}
for i, plot in pairs(plots:GetChildren()) do
table.insert(plotsToSelect, plot)
end
for i, plot in pairs(plotsToSelect) do
print(plot)
end
local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local CtfGameGui = PlayerGui:WaitForChild("SharedGui"):WaitForChild("RedRockStarterGui"):WaitForChild("CtfGame")
local ScoreboardFrame = CtfGameGui:WaitForChild("ScoreboardFrame")
local Header = ScoreboardFrame:WaitForChild("Header")
local FooterFrame = ScoreboardFrame:WaitForChild("FooterFrame")
local ExitBtn = FooterFrame:WaitForChild("ExitBtn")
local TimerLabel = FooterFrame:WaitForChild("Timer")
I chain the waits as a move through the hierarchy. Obviously it doesn’t have to be all spelled out like this if you have a simpler structure or requirements but this definitely works to access the UI elements e.g. I can set the Text on TimerLabel here with no issues.
My bad, I should’ve clarified, my local script is trying to loop through the children of a group workspace, not playergui.
--local script in playergui
local plots = workspace:WaitForChild("HousePlots")
local plotsToSelect = {}
for i, plot in pairs(plots:GetChildren()) do
table.insert(plotsToSelect, plot)
end
for i, plot in pairs(plotsToSelect) do
print(plot)
end
Sometime it will print all children, sometimes none, or just some.
In this case, I think something will have to tell you how many there are to load…
Something like this should work:
--local script in playergui
local HOUSE_PLOTS_COUNT = 10
local loadedPlots = 0
local plots = workspace:WaitForChild("HousePlots")
local plotsToSelect = {}
while loadedPlots < HOUSE_PLOTS_COUNT do
loadedPlots = #plots:GetChildren()
wait(0.1)
end
for i, plot in pairs(plots:GetChildren()) do
table.insert(plotsToSelect, plot)
end
I’m not a fan of the dependency on the HOUSE_PLOTS_COUNT const but something has to tell the client when they’ve “loaded the right things”. This is simpler than doing server call handshakes but might not work if the children are dynamically generated and not a known count at startup time.