Player spawning before Main Menu

Hi,

I’m trying to create a main menu for one of my games. The idea is to have the main menu use a camera part facing a mesh, with a few buttons displayed in the screen GUI until the player clicks the (Continue) button, which will then spawn them.

The issue I’m running into is that when I run the game the player spawns first along with its default camera before loading the actual main menu and its camera.

I tried fixing this by disabling CharacterAutoLoads, so that the player doesn’t spawn until they click “Continue”. I tried this and get an “Infinite yield possible” warning in the output, and the main menu doesn’t show.


StarterPlayerScripts:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local spawnEvent = ReplicatedStorage:WaitForChild("SpawnPlayer")

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

camera.CameraType = Enum.CameraType.Scriptable
local mainMenuCamPart = workspace:WaitForChild("Menu"):WaitForChild("MainMenuCam")
camera.CFrame = mainMenuCamPart.CFrame

local playerGui = player:WaitForChild("PlayerGui")
local menuGui = playerGui:WaitForChild("MenuGui")
local Background = menuGui:WaitForChild("Background")

local continueButton = Background:WaitForChild("ContinueButton")
local characterButton = Background:FindFirstChild("CharacterButton")
local aboutButton = Background:FindFirstChild("AboutButton")

continueButton.MouseButton1Click:Connect(function()
    RunService:UnbindFromRenderStep("CameraUpdate")
    spawnEvent:FireServer()
    
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")
    
    camera.CameraType = Enum.CameraType.Custom
    camera.CameraSubject = humanoid
    menuGui:Destroy()
end)

if characterButton then
    characterButton.MouseButton1Click:Connect(function()
        if aboutButton then aboutButton.Visible = false end
        continueButton.Visible = false
        characterButton.Visible = false
    end)
end

if aboutButton then
    aboutButton.MouseButton1Click:Connect(function()
    end)
end

local baseCFrame = mainMenuCamPart.CFrame
local maxAngle = math.rad(5)

RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
    local mouseLocation = UIS:GetMouseLocation()
    local viewportSize = camera.ViewportSize

    local angleX = -((mouseLocation.Y / viewportSize.Y) * 2 - 1) * maxAngle
    local angleY = -((mouseLocation.X / viewportSize.X) * 2 - 1) * maxAngle

    camera.CFrame = baseCFrame * CFrame.Angles(angleX, angleY, 0)
end)

UIS.MouseIcon = "rbxassetid://11232270592"

local blackScreen = Instance.new("ScreenGui")
blackScreen.Name = "BlackScreen"
blackScreen.IgnoreGuiInset = true
blackScreen.Parent = playerGui

local frame = Instance.new("Frame")
frame.Size = UDim2.new(1, 0, 1, 0)
frame.Position = UDim2.new(0, 0, 0, 0)
frame.BackgroundColor3 = Color3.new(0, 0, 0)
frame.BorderSizePixel = 0
frame.Parent = blackScreen

local function fadeOutBlackScreen()
    local tweenInfo = TweenInfo.new(4, Enum.EasingStyle.Linear)
    local frameTween = TweenService:Create(frame, tweenInfo, {BackgroundTransparency = 1})
    frameTween:Play()

    frameTween.Completed:Connect(function()
        blackScreen:Destroy()
    end)
end

task.wait(0.5)
fadeOutBlackScreen()

Spawnplayer script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local spawnEvent = ReplicatedStorage:WaitForChild("SpawnPlayer")

spawnEvent.OnServerEvent:Connect(function(player)
	if not player.Character then
		player:LoadCharacterAsync()
	end
end)

The UI doesn’t show because, well… it doesn’t show until they spawn. You will have to make it so that UI is coded from ReplicatedFirst.

2 Likes