Local script stuck on game.Loaded:Wait()

Hello developers!

I’ve been working on implementing a custom loading screen, however the script gets stuck yielding on game.Loaded:Wait().

Here is the code:

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

Is the game has already loaded and game:IsLoaded() returns true it continues as normal however if it hasn’t it just gets stuck on

game.Loaded:Wait()
5 Likes

Is the script in ReplicatedFirst?

1 Like

It’s in StarterGui. Does the script need to be I replicated first in order for this to work?

1 Like

I’m not entirely sure, but I usually use game.Loaded in ReplicatedFirst.

I tried it and it still works, mind showing your hierarchy?

The script I used:

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

print("hey") -- "hey" (still prints)

in game.StarterGui

Just letting you know incase this matters, the scripts parent is a ScreenGui, this ScreenGui’s parent is StarterGui. Also it loads properly if the if not game:IsLoaded() returns true. It’s only if the game hasn’t loaded yet where the issue arises.

Here is the rest of the code prior to the game:IsLoaded():

local ReplicatedFirst = game:GetService("ReplicatedFirst")
local loadingFrame = script.Parent:WaitForChild('loadingFrame')
local menuFrame = loadingFrame.Parent:FindFirstChild('menu')
local blankFrame = menuFrame.Parent.blankFrame
local tweenService = game:GetService('TweenService')
local rn = game:GetService('RunService')
local CP = game:GetService('ContentProvider')
local event = game.ReplicatedStorage:WaitForChild('Events'):WaitForChild('loaded')

local buttons = menuFrame:FindFirstChild('Buttons')

local started = false

loadingFrame.Visible = true
menuFrame.Visible = true

ReplicatedFirst:RemoveDefaultLoadingScreen()

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

print("Game has loaded") -- Doesn't print.

Prints for me
Screenshot 2024-03-23 230940

I think your game may have loaded before the if not game:IsLoaded(), because it works for me if it has but if it hasn’t then it just gets stuck at game.Loaded:Wait.

1 Like

can you take a picture of your hierarchy so i can take a better look?

If you mean the rest of the script, here you go:

local ReplicatedFirst = game:GetService("ReplicatedFirst")
local loadingFrame = script.Parent:WaitForChild('loadingFrame')
local menuFrame = loadingFrame.Parent:FindFirstChild('menu')
local blankFrame = menuFrame.Parent.blankFrame
local tweenService = game:GetService('TweenService')
local rn = game:GetService('RunService')
local event = game.ReplicatedStorage:WaitForChild('Events'):WaitForChild('loaded')

local buttons = menuFrame:FindFirstChild('Buttons')

local started = false

loadingFrame.Visible = true
menuFrame.Visible = true

ReplicatedFirst:RemoveDefaultLoadingScreen()

event.Event:Wait()

local startScreenFold = workspace:WaitForChild('startScreen')
local mainCam = startScreenFold.mainCam

task.wait(1)

local goals = {
	["Ani1"] = {mainCam.CFrame, startScreenFold.goalOne.CFrame};
	["Ani2"] = {startScreenFold.goalTwoStart.CFrame, startScreenFold.goalTwoEnd.CFrame};
	["Ani3"] = {startScreenFold.goalThreeStart.CFrame, startScreenFold.goalThreeEnd.CFrame};
}

print('starting')

local function setVisible()
	for i, v in pairs(loadingFrame:GetChildren()) do
		for i = 1,20 do
			loadingFrame.Transparency += 0.05
			v.Transparency += 0.05
			task.wait()
		end
		loadingFrame:Destroy()
		task.wait(1)
	end
end

local function tween(goal, dur)
	print('tweening')
	local info = TweenInfo.new(
		dur,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false
	)
	
	local goal = {
		CFrame = goal
	}
	
	local tween = tweenService:Create(mainCam, info, goal)
	tween:Play()
	
	tween.Completed:Wait()
end

task.wait(2)

spawn(setVisible)

local cam = workspace.Camera
cam.CameraType = Enum.CameraType.Scriptable

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

char.Humanoid.WalkSpeed = 0

coroutine.wrap(function()

	while started == false do
		local speed = 5
		for i, v in pairs(goals) do
			print('setting')
			mainCam.CFrame = v[1]
			print('set')
			tween(v[2], speed)
		end
		task.wait()
	end
	
	task.wait()
end)()

local connection = rn.RenderStepped:Connect(function()
	cam.CFrame = mainCam.CFrame
end)

buttons.playButton.MouseButton1Up:Connect(function()
	for i = 1,20 do
		blankFrame.Transparency -= 0.05
		task.wait(0.05)
	end
	
	task.wait(1)
	char.Humanoid.WalkSpeed = 16
	started = true
	cam.CameraType = Enum.CameraType.Custom
	connection:Disconnect()
	menuFrame:Destroy()
	
	for i = 1,20 do
		blankFrame.Transparency += 0.05
		task.wait(0.05)
	end
	
	script.Parent:Destroy()
end)


1 Like

wait wheres the game.loaded part?

I was about to mention that lol, I’m trying to see if putting a script in replicated first, then waiting for it to load, and then firing an event once it has, will fix it.

1 Like

Okay so the script in ReplicatedFirst, loads the game and fires the event?

If so, then that might not be an efficient, don’t do that way.

Ok, I’m probably just going to wait like 10 seconds instead of using the game.Loaded:Wait() because it doesn’t seem to work.

Maybe, just don’t put anything? 10 seconds feels too long.

Its likely going too be a large map when completed so 10 seconds to be safe.

1 Like

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