スクリプトを追加

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

これらすべての作業を一緒にもたらす時が来ました!ビームとパーティクルコンポーネントを作成したので、プリメイドのスクリプトを 3つ追加します。これらのスクリプトは、コンポーネントに何をすべきかを告知して、チュートリアルを管理します。たとえば、スクリプトは新しいプレイヤーにビームを作成し、ゴールと対話するたびに粒子を放出します。

ビームと粒子を保存する

スクリプトを追加する前に、ビームとパーティクルは、スクリプトが必要に応じてコピーを作成できる場所に移動する必要があります。

  1. In ReplicatedStorage で、 PlayerTutorial という新しいフォルダを作成します。TutorialBeamをテストプレイヤーから新しいフォルダに移動し、新しいフォルダに移動します。

  2. In ServerStorage で、 TutorialParticles という名前のフォルダを作成します。 バースト パーティクルを TestPlayer からそのフォルダに移動します。

  3. ビームとパーティクルエミッタが移動したら、テストプレイヤーはもう必要ありません。 テストプレイヤーを削除する スクリプトが完了すると、実際のプレイヤーと一緒に動作するため。

イベントを作成

プレイヤーがゴールと対話するたびに、チュートリアルスクリプトはそのプレイヤーの進捗状況を更新し、粒子効果を放つことができるように知る必要があります。スクリプトを通知するには、 イベント を使用してシグナルを送信できます。

  1. ReplicatedStorage > PlayerTutorial で、2つの リモートイベント オブジェクトを作成し、名前を 次のゴールチュートリアル終了 に設定します。

スクリプトを追加する

以下の 3つのスクリプトは、以前に作成されたパーティクルエミッターとビームオブジェクトを検索し、チュートリアルシステムを管理します。

  1. ReplicatedStorage > PlayerTutorial > で、新しい モジュールスクリプト を名前付きの TutorialManager を作成します。

    デフォルトコードを置き換えるには、以下のコード全体をコピーして貼り付けます。


    local TutorialManager = {}
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local tutorialFolder = ReplicatedStorage:WaitForChild("PlayerTutorial")
    local TutorialEndEvent = tutorialFolder:WaitForChild("TutorialEnd")
    local NextGoalEvent = tutorialFolder:WaitForChild("NextGoal")
    -- ノートゴールパーツはテーブルで注文する必要があり、そうでなければゲーム内のゴール注文が異なる可能性があります
    local goalParts = {
    workspace.TutorialGoals.GoalPart1,
    workspace.TutorialGoals.GoalPart2
    }
    local function checkTutorialEnd(player, goalParts)
    local currentIndex = player:WaitForChild("GoalProgress")
    return currentIndex.Value >= #goalParts
    end
    local function finishTutorial(player)
    local playerBeam = player.Character.HumanoidRootPart:FindFirstChildOfClass("Beam")
    playerBeam:Destroy()
    print(player.Name .. " finished the tutorial")
    -- 以降のコードのプレースホルダー。例えば、他のタスクを実行するためにサーバーにメッセージを送信したい場合
    end
    function TutorialManager.interactGoal(player)
    NextGoalEvent:FireServer()
    end
    function TutorialManager.getTutorialGoals()
    return goalParts
    end
    function TutorialManager.nextGoal(player, goalParts)
    if checkTutorialEnd(player, goalParts) then
    finishTutorial(player)
    else
    -- プレイヤーのゴールトラッカーを増加
    local currentGoalIndex = player:WaitForChild("GoalProgress")
    currentGoalIndex.Value += 1
    end
    end
    -- チュートリアルゴールを通じてプレイヤーの進捗状況をローカルで追跡するための int 値を作成する
    function TutorialManager.setupPlayerProgress(player)
    local currentGoalProgress = Instance.new("IntValue")
    currentGoalProgress.Name = "GoalProgress"
    currentGoalProgress.Value = 1
    currentGoalProgress.Parent = player
    end
    return TutorialManager

    このスクリプトは、チュートリアルでプレイヤーの進捗を管理するコードを実行します。これには、ゴールと対話するためのコードの実行や、チュートリアルが終了したときに起こることなどが含まれます。

  2. In サーバースクリプトサービス , create a new スクリプト named TutorialParticles .

    以下のコードを貼り付けます。


    local Players = game:GetService("Players")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local ServerStorage = game:GetService("ServerStorage")
    local tutorialFolder = ReplicatedStorage:WaitForChild("PlayerTutorial")
    local NextGoalEvent = tutorialFolder:WaitForChild("NextGoal")
    local EMIT_RATE = 50
    local function playParticleBurst(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    local particleAttachment = humanoidRootPart:WaitForChild("ParticleAttachment")
    -- 添付ファイル上のパーティクルを通過し、パーティクルの種類に従って再生する
    for _, particle in particleAttachment:GetChildren() do
    if particle:IsA("ParticleEmitter") then
    particle:Emit(EMIT_RATE)
    end
    end
    end
    local function setupPlayerParticles(player)
    player.CharacterAdded:Connect(function(character)
    local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    local playerParticleAttachment = Instance.new("Attachment")
    playerParticleAttachment.Name = "ParticleAttachment"
    playerParticleAttachment.Parent = humanoidRootPart
    -- フォルダ内のパーティクルをクローンし、1つ以上あってもプレイヤーに付属する
    for _, emitter in ServerStorage.TutorialParticles:GetChildren() do
    emitter:Clone().Parent = playerParticleAttachment
    end
    end)
    end
    Players.PlayerAdded:Connect(setupPlayerParticles)
    NextGoalEvent.OnServerEvent:Connect(playParticleBurst)

    このスクリプトは、プレイヤーがゴールと対話するたびにバーストパーティクルを再生します。また、インタラクション中に生成されるパーティクルの数を決定する変数 EMIT_RATE があります。

  3. In StarterPlayer > StarterPlayerScripts, create a new ローカルスクリプト named チュートリアルスクリプト .

    次に、以下のスクリプトを貼り付けます。このスクリプトは、プレイヤーを誘導するビームを作成し、管理します。


    local Players = game:GetService("Players")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local tutorialFolder = ReplicatedStorage:WaitForChild("PlayerTutorial")
    local TutorialManager = require(tutorialFolder:WaitForChild("TutorialManager"))
    local TutorialEndEvent = tutorialFolder:WaitForChild("TutorialEnd")
    local player = Players.LocalPlayer
    local goalParts = TutorialManager.getTutorialGoals()
    local playerBeam = nil
    local goalIndex = nil
    local function getTargetAttachment()
    local currentTarget = goalParts[goalIndex.Value]
    local interactionPart = currentTarget:FindFirstChild("InteractionPart")
    local attachment = interactionPart and interactionPart:FindFirstChildOfClass("Attachment")
    if not attachment then
    attachment = Instance.new("Attachment")
    attachment.Name = "BeamAttachment"
    attachment.Parent = currentTarget
    end
    return attachment
    end
    local function updateBeamTarget()
    playerBeam = player.Character.HumanoidRootPart:FindFirstChildOfClass("Beam")
    local targetBeamAttachment = getTargetAttachment()
    if targetBeamAttachment then
    playerBeam.Attachment1 = targetBeamAttachment
    else
    warn("Attachment not found in a goal. Check that goals have attachments or they're included under the InteractionPart")
    end
    end
    local function setupGoals()
    for _, part in goalParts do
    local interactionPart = part:FindFirstChild("InteractionPart")
    local proximityPrompt = interactionPart and interactionPart:FindFirstChild("ProximityPrompt")
    if proximityPrompt then
    proximityPrompt.Triggered:Connect(function(player)
    proximityPrompt.Enabled = false
    TutorialManager.nextGoal(player, goalParts)
    TutorialManager.interactGoal(player)
    end)
    else
    warn("Proximity prompt not included in goal. Add one to each goal part under the InteractionPart")
    end
    end
    end
    local function createBeamForCharacter(character)
    local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    local playerBeamAttachment = Instance.new("Attachment")
    local beamTemplate = tutorialFolder:WaitForChild("TutorialBeam")
    if not beamTemplate then
    warn("Tutorial Beam not found in ReplicatedStorage")
    end
    playerBeamAttachment.Name = "BeamAttachment"
    playerBeamAttachment.Parent = humanoidRootPart
    local targetBeamAttachment = getTargetAttachment()
    playerBeam = beamTemplate:Clone()
    playerBeam.Attachment0 = playerBeamAttachment
    playerBeam.Attachment1 = targetBeamAttachment
    playerBeam.Enabled = true
    playerBeam.Parent = humanoidRootPart
    end
    local function setupPlayer()
    setupGoals()
    TutorialManager.setupPlayerProgress(player)
    goalIndex = player:WaitForChild("GoalProgress")
    player.CharacterAdded:Connect(createBeamForCharacter)
    if player.Character then
    createBeamForCharacter(player.Character)
    end
    end
    setupPlayer()
    goalIndex.Changed:Connect(updateBeamTarget)
  4. プロジェクトを再生して、スクリプトをテストします。インタラクション機能を使用して、コードが機能するかどうかを確認します。

トラブルシュートのヒント

問題 : ゲームが開始すると、パーティクルが再生します。

  • サーバーストレージ > チュートリアルパーティクル > バーストに移動します。[オフにする] が有効になっていることを確認します。 問題 : コンパイラーでの「無限の生産」などの警告

  • スクリプトが特定の場所で特定のオブジェクトを検索しているため、パーツの名前が間違っている可能性があります。ゲーム内の各パーツの名前と場所がチュートリアルと一致しているかどうかを二度確認します。

スクリプトの利点と制限

このチュートリアルシステムをエクスペリエンスで使用している場合は、フォロー中のことを覚えておいてください: 利点

  • チュートリアル終了などのイベントは、他のスクリプトをトリガーするのに使用できます。たとえば、このイベントが発動すると、プレイヤーに特別なアイテムを授与できます。
  • チュートリアルパーティクルスクリプトは、一度に複数のパーティクルを再生できます。より複雑な効果のために、ServerStorage/TutorialParticles により多くのパーティクルを追加できます。 制限
  • チュートリアル中のプレイヤーの進行状況は永続しません、つまりその進行状況を保存する方法をコード化する必要があります。ガイドについては、「データの保存」を参照してください。