Wait for game to load before executing code?

How come Roblox allows code to execute before the loading screen is gone? I need my code to only execute after this screen disappears as I have an intro which plays at the start of the game and I don’t want players to miss it.

2 Likes

Are you using RemoveDefaultLoadingScreen?

Refer to Custom Loading Screens for more information. In essence:

  • Wait for the PlayerGui to exist
  • Parent the ScreenGui from ReplicatedFirst to PlayerGui
  • Remove the default loading screen

Make sure to put stuff you want to load first in ReplicatedFirst

6 Likes

No, this happens in all games, but I need to know if there is a way of preventing it in my own game.

Edited my post for more information.

In CoreGui there is a ScreenGui named ‘RobloxLoadingGui’, and inside it is a frame called ‘BlackFrame’. When the loading GUI is finished, the contents of the ScreenGui are deleted, so you could listen for that – alternatively, you could listen for changes to BlackFrame’s transparency for a quicker response (not delayed by waiting for the BlackFrame transparency tween to finish).

2 Likes

This seems like bad practice. I would recommend adding a black overlay to the PlayerGui instead, then calling RemoveDefaultLoadingScreen, and then doing whatever transitions you wanna do. Or just calling RemoveDefaultLoadingScreen on its own to remove it without transitions. That way you don’t need to rely on a certain layout of the core UI that could change without notice if Roblox decides to update it.

If you’re interested, this is the logic of the internal corescript in pseudocode:

if ReplicatedFirst has any children then
	wait(5)
	remove the loading screen (if the user hasn't with RemoveDefaultLoadingScreen)
else
	if game:IsLoaded() then
		remove the loading screen
	else
		game.Loaded:Connect(function()
			remove the loading screen
			disconnect self
		end)
	end
end
5 Likes

Yeah true, was just thinking of a way to let the loading screen run its course without interfering with the default loading screen while still accomplishing the OP’s goals.

Forgot about game.Loaded though. I guess the other method would be simply listening to game.Loaded signal. It’s still dependant on how the CoreGui functions but is good enough.

1 Like

local function functionToRunWhenTheGameFullyLoads()
– Put Loading screen code here because this functions runs after the game is fully loadded !
end
while not game:IsLoaded() do
task.wait()
end
functionToRunWhenTheGameFullyLoads()