
Photo by
kiraku_san
RobloxStudioで、ゲーム開始時のローディング画面を作る方法
前回の記事
さて、今回はゲームが始まる前のあのローディング画面を作ってみました。
こんな感じでゲーム開始時に表示されます。

Skipを押せばローディングを待たずにゲームが出来ます。
作成した環境が重くなかったので一瞬で終わりましたが、重い環境であれば、%が少しずつ増えていくと思います。
ReplicatedFirstの下にLocalScriptを置いて、以下を記入すれば完了です。
コードはこちらです。
local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ContentProvider = game:GetService("ContentProvider")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
-- ローディングスクリーンの作成
local screenGui = Instance.new("ScreenGui")
screenGui.IgnoreGuiInset = true
-- 背景(暗すぎないように半透明)
local background = Instance.new("Frame")
background.Size = UDim2.new(1, 0, 1, 0)
background.BackgroundColor3 = Color3.fromRGB(0, 20, 40)
background.BackgroundTransparency = 0.5
background.Parent = screenGui
-- ローディングテキスト
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(1, 0, 0.1, 0)
textLabel.Position = UDim2.new(0, 0, 0.45, 0)
textLabel.BackgroundTransparency = 1
textLabel.Font = Enum.Font.GothamMedium
textLabel.TextColor3 = Color3.new(0.8, 0.8, 0.8)
textLabel.Text = "Loading"
textLabel.TextSize = 28
textLabel.Parent = screenGui
-- スキップボタン
local skipButton = Instance.new("TextButton")
skipButton.Size = UDim2.new(0.2, 0, 0.05, 0)
skipButton.Position = UDim2.new(0.4, 0, 0.8, 0)
skipButton.BackgroundColor3 = Color3.fromRGB(100, 100, 100)
skipButton.Font = Enum.Font.GothamBold
skipButton.TextColor3 = Color3.new(1, 1, 1)
skipButton.Text = "Skip"
skipButton.TextSize = 24
skipButton.Parent = screenGui
-- デフォルトのローディングスクリーンを消す
ReplicatedFirst:RemoveDefaultLoadingScreen()
-- プレイヤーの移動を禁止する関数
local function disablePlayerMovement(player)
local function freezeCharacter(character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.WalkSpeed = 0 -- 移動速度を0にする
humanoid.JumpPower = 0 -- ジャンプ力を0にする
end
end
-- すでにキャラクターが存在するなら適用
if player.Character then
freezeCharacter(player.Character)
end
-- キャラクターがリスポーンした場合も適用
player.CharacterAdded:Connect(freezeCharacter)
-- すべての入力をブロック
ContextActionService:BindAction("DisableControls", function()
return Enum.ContextActionResult.Sink
end, false, Enum.UserInputType.Keyboard, Enum.UserInputType.Gamepad1, Enum.UserInputType.MouseMovement)
end
-- プレイヤーの移動を許可する関数
local function enablePlayerMovement(player)
local function unfreezeCharacter(character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.WalkSpeed = 16 -- 通常の移動速度に戻す
humanoid.JumpPower = 50 -- 通常のジャンプ力に戻す
end
end
-- キャラクターが存在するなら適用
if player.Character then
unfreezeCharacter(player.Character)
end
-- `CharacterAdded` のイベントを解除(別の関数で管理する場合は不要)
player.CharacterAdded:Connect(unfreezeCharacter)
-- 入力のブロックを解除
ContextActionService:UnbindAction("DisableControls")
end
-- プレイヤー取得
local player = Players.LocalPlayer
if player then
disablePlayerMovement(player) -- ロード中に動けないようにする
end
-- プレイヤーのGuiにローディング画面を配置
local playerGui = player:WaitForChild("PlayerGui")
screenGui.Parent = playerGui
-- スキップフラグ
local skipLoading = false
-- スキップボタンの処理
skipButton.MouseButton1Click:Connect(function()
skipLoading = true
screenGui:Destroy()
enablePlayerMovement(player) -- スキップ後に移動を許可
end)
-- ゲームのロード処理
local assets = workspace:GetDescendants()
for i, asset in ipairs(assets) do
if skipLoading then break end -- スキップ時にループ終了
wait(0.001)
ContentProvider:PreloadAsync({asset})
local percentage = math.floor(i / #assets * 100)
textLabel.Text = "Loading " .. percentage .. "%"
end
wait(2)
-- もしスキップされていなかったら、最後に1秒待って削除
if not skipLoading then
wait(1)
screenGui:Destroy()
enablePlayerMovement(player) -- ロード完了後に移動を許可
end