Function run when game loads

Is there a way to make the Intro code run when the user’s game has loaded? This code makes it so there’s a slight delay for the camera to apply and it starts before the game itself may have loaded. I’ve seen game.Loaded doesn’t quite work for that. The following local script is in StarterPlayerScripts.

local StarterGui = script.Parent.Parent:WaitForChild("PlayerGui")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local Camera = workspace.CurrentCamera

local MainUI = StarterGui:WaitForChild("MainUI")
local Black = MainUI:WaitForChild("Black")

local Computer = workspace:WaitForChild("Computer")
local CameraPart = workspace:WaitForChild("CameraPart")

local Player =  Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local function Intro()
	
	Black.Visible = true
	
	Camera.CameraSubject = CameraPart
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = CameraPart.CFrame
	
	TweenService:Create(Camera,TweenInfo.new(3,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false),{CFrame = Camera.CFrame * CFrame.new(Vector3.new(0,0,-7))}):Play()
	
	TweenService:Create(Black,TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false),{BackgroundTransparency = 1}):Play()
end

RunService.Heartbeat:Wait()
Intro()

repeat task.wait() until game:IsLoaded()
This wouldn’t work? (Put at the top of ur code)

I think this could work, but i’ve seen people say that it isn’t good to do this so i think there may be a better solution

If you’re looking to wait until the map loads, you can try using ContentProvider:PreloadAsync()

1 Like

Thanks, i’ll try that. 40 characters

@ErrorBuilds probably has the answer youre looking for, unless Im misunderstanding your question.

you could probably throw something together like

function Load()
	game:GetService('ContentProvider'):PreloadAsync({game})
end	
Load()

, which would have the Content Provider yield for every single descendant of its parameter, in this case your game, until they load. PreloadAsync is often used for its to priority-track-ing ability to prioritize certain things for loading screens.

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