新增腳本

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

是時候把這一切工作結合起來了!現在你創建了光束和粒子組件,你將添加三個預製腳本。這些腳本管理教學,告訴組件何時做什麼。例如,腳本會為新玩家創建光束,並在他們與目標互動時發出粒子。

儲存光束和粒子

在添加腳本之前,光束和粒子需要移動到可以根據需要複製它們的地方。

  1. 複製儲存 中,創建一個新的文件夾名為 PlayerTutorial 。將 TutorialBeam 從測試玩家移出,並移至新文件夾。

  2. 伺服器儲存 中,創建名為 TutorialParticles 的文件夾。將 爆破 粒子從測試玩家移出到該文件夾。

  3. 一旦光束和粒子發射器被移動,您就不再需要測試玩家。 刪除 測試玩家,因為完成後會與真實玩家一起工作。

創建事件

每次玩家與目標互動時,教學腳本需要知道,以便可以更新該玩家的進度並發出粒子效果。要通知腳本,信號可以使用 事件 傳送。

  1. 在 ReplicatedStorage > PlayerTutorial 中,創建兩個 遠端事件 對象。命名為 下一個目標教學結束

添加指令碼

以下三個腳本將尋找先前創建的粒子發射器和光束對象,並管理教學系統。

  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. 伺服器腳本服務 中,創建一個新的 腳本 名為 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
    -- 在文件夾中複製粒子,即使有多於一個也一樣會附加到玩家
    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. 在 StarterPlayer > StarterPlayerScripts 中,創建一個新的 本地腳本 名為 教學腳本 的新腳本。

    然後,貼上下面的腳本。這個腳本創建和管理用於引導玩家的光束。


    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. 播放項目以測試腳本。使用互動功能移動從一個攤位到另一個攤位,以查看代碼是否正常運作。

排除故障提示

問題 : 遊戲開始時會播放粒子。

  • 前往伺服器儲存 > 教學粒子 > 爆炸。檢查啟用為關閉。 問題 : 譯譯器中的警告,例如「無限收集」。

  • 因為腳本正在尋找特定位置的特定對象,所以有可能是一個部分的名稱不正確。雙檢查每個部分的名稱和位置是否與教學匹配。

腳本優點和限制

如果您在體驗中使用此教學系統,請記住以追蹤中事項: 好處

  • 例如教學結束等事件可以用來觸發其他腳本。例個體、實例,當這個事件發生時,你可以獎勵玩家一個特殊物品。
  • 教學粒子腳本可以一次播放多個粒子。您可以在伺服器儲存/教學參數中添加更多粒子來獲得更複雜的效果。 限制
  • 教學中玩家的進度不會永久,這意味著你必須寫一些方法來保存那些進度。如需指導,請參閱文章:儲存資料。