How to actually wait for object to be loaded without infinite yield warnings

This is placed inside RepFirst (it’s an intro UI setup)

local SharedModules = ReplicatedStorage:WaitForChild('SharedModules')
local UIModules = SharedModules:WaitForChild('UIModules')
local Click = require(UIModules.Click)

and I keep getting infinite yield warnings for the SharedModules line. So I add in a timeout

local SharedModules = ReplicatedStorage:WaitForChild('SharedModules', 5)

However, I then get errors for the lines below (as SharedModules obviously still hasn’t loaded yet, thus it returns nil)

So what am I supposed to do? I either get infinite yields, or errors. It does eventually load in and what not, but I still don’t like the warning appearing in my dev console + it creates other infinite yield problems (I don’t know why)

5 Likes

The warnings you are seeing are Roblox CoreScript warnings. You can’t do anything about these.

However if you want to prevent this issue in the future for your own code you can use a timeout of math.huge

local SharedModules = ReplicatedStorage:WaitForChild("SharedModules", math.huge)
local UIModules = SharedModules:WaitForChild("UIModules", math.huge)

Demonstration:
image

2 Likes

I dont know and I am guessing, but maybe u could try with pcalls and while pcall is not success wait some time and try again

1 Like

To solve this type of problems I suggest to wait till its loaded. Like

script.Parent:RemoveDefaultLoadingScreen()


local loadingScreen = script.LoadingScreenGUI

loadingScreen.Parent = game.Players.LocalPlayer.PlayerGui


local contenProvider = game:GetService("ContentProvider")
game.Loaded:Wait()
local toLoad = workspace:GetDescendants()
local total = #toLoad
for i,v in pairs(toLoad) do
	loadingScreen.Background.Counter.Text = "Loading: " .. i .. "/" .. total .. " " .. v.Name
	loadingScreen.Background.BarBG.Bar.Size = UDim2.new(i/total, 0, 1, 0)
	contenProvider:PreloadAsync({v})
end


wait(1)
loadingScreen:Destroy()

It’s a loading gui script. If the script runs the loading gui won’t destroyed until it’s finished loading. Maybe you can try something like this.

2 Likes

You can make simply a WaitForChild function like this:

local function WaitForChild(Object,Name,Time)
       local FindObject = Object:FindFirstChild(Name)
       local StartTime = tick()
       local EndTime = (Time or math.huge) + StartTime
       while not FindObject do
             if not tick() >= EndTime then
             FindObject = Object:FindFirstChild(Name)
             else
              break
             end
            wait()
      end
      return FindObject
end

The function above isn’t tested, say if there is any error.

Maybe you need to think about where you place your assets (modulescripts, etc.), according to how Roblox’s network-replication-method works, and in the sequence that clients needs them during the initial loading.

You did write (thats why I highlighted it) “placed inside ReplicatedFirst”, yet that code, you then have instructed to depend on an assert from ReplicatedStorage, which is “slower” to replicate - depending on how much “data” you have in Workspace, ReplicatedFirst & ReplicatedStorage, that is being replicated to clients when they join the game.

So perhaps move your SharedModules (and its children) into the ReplicatedFirst area? (And update all the other scripts that needs to require it.)

Or increase the timeout value for {instance}:WaitForChild() to much more than just 5 seconds?