是時候把這一切工作結合起來了!現在你創建了光束和粒子組件,你將添加三個預製腳本。這些腳本管理教學,告訴組件何時做什麼。例如,腳本會為新玩家創建光束,並在他們與目標互動時發出粒子。
儲存光束和粒子
在添加腳本之前,光束和粒子需要移動到可以根據需要複製它們的地方。
在 複製儲存 中,創建一個新的文件夾名為 PlayerTutorial 。將 TutorialBeam 從測試玩家移出,並移至新文件夾。
在 伺服器儲存 中,創建名為 TutorialParticles 的文件夾。將 爆破 粒子從測試玩家移出到該文件夾。
一旦光束和粒子發射器被移動,您就不再需要測試玩家。 刪除 測試玩家,因為完成後會與真實玩家一起工作。
創建事件
每次玩家與目標互動時,教學腳本需要知道,以便可以更新該玩家的進度並發出粒子效果。要通知腳本,信號可以使用 事件 傳送。
在 ReplicatedStorage > PlayerTutorial 中,創建兩個 遠端事件 對象。命名為 下一個目標 和 教學結束 。
添加指令碼
以下三個腳本將尋找先前創建的粒子發射器和光束對象,並管理教學系統。
在 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 >= #goalPartsendlocal function finishTutorial(player)local playerBeam = player.Character.HumanoidRootPart:FindFirstChildOfClass("Beam")playerBeam:Destroy()print(player.Name .. " finished the tutorial")-- 用於進一步代碼的暫時器。例如,如果您想向服務器發送消息以執行其他任務endfunction TutorialManager.interactGoal(player)NextGoalEvent:FireServer()endfunction TutorialManager.getTutorialGoals()return goalPartsendfunction TutorialManager.nextGoal(player, goalParts)if checkTutorialEnd(player, goalParts) thenfinishTutorial(player)else-- 增加玩家的目標追蹤器local currentGoalIndex = player:WaitForChild("GoalProgress")currentGoalIndex.Value += 1endend-- 創建一個 int 值來本地跟蹤玩家在教學目標通過的進度function TutorialManager.setupPlayerProgress(player)local currentGoalProgress = Instance.new("IntValue")currentGoalProgress.Name = "GoalProgress"currentGoalProgress.Value = 1currentGoalProgress.Parent = playerendreturn TutorialManager這個腳本執行教學中管理玩家進度的代碼。這包括與目標互動的執行代碼或教學結束時發生的事情。
在 伺服器腳本服務 中,創建一個新的 腳本 名為 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 = 50local 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() doif particle:IsA("ParticleEmitter") thenparticle:Emit(EMIT_RATE)endendendlocal 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() doemitter:Clone().Parent = playerParticleAttachmentendend)endPlayers.PlayerAdded:Connect(setupPlayerParticles)NextGoalEvent.OnServerEvent:Connect(playParticleBurst)這個腳本會在玩家與目標互動時播放爆破粒子。還有一個名為 EMIT_RATE 的變量,決定在互動期間生成多少粒子。
在 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.LocalPlayerlocal goalParts = TutorialManager.getTutorialGoals()local playerBeam = nillocal goalIndex = nillocal function getTargetAttachment()local currentTarget = goalParts[goalIndex.Value]local interactionPart = currentTarget:FindFirstChild("InteractionPart")local attachment = interactionPart and interactionPart:FindFirstChildOfClass("Attachment")if not attachment thenattachment = Instance.new("Attachment")attachment.Name = "BeamAttachment"attachment.Parent = currentTargetendreturn attachmentendlocal function updateBeamTarget()playerBeam = player.Character.HumanoidRootPart:FindFirstChildOfClass("Beam")local targetBeamAttachment = getTargetAttachment()if targetBeamAttachment thenplayerBeam.Attachment1 = targetBeamAttachmentelsewarn("Attachment not found in a goal. Check that goals have attachments or they're included under the InteractionPart")endendlocal function setupGoals()for _, part in goalParts dolocal interactionPart = part:FindFirstChild("InteractionPart")local proximityPrompt = interactionPart and interactionPart:FindFirstChild("ProximityPrompt")if proximityPrompt thenproximityPrompt.Triggered:Connect(function(player)proximityPrompt.Enabled = falseTutorialManager.nextGoal(player, goalParts)TutorialManager.interactGoal(player)end)elsewarn("Proximity prompt not included in goal. Add one to each goal part under the InteractionPart")endendendlocal function createBeamForCharacter(character)local humanoidRootPart = character:WaitForChild("HumanoidRootPart")local playerBeamAttachment = Instance.new("Attachment")local beamTemplate = tutorialFolder:WaitForChild("TutorialBeam")if not beamTemplate thenwarn("Tutorial Beam not found in ReplicatedStorage")endplayerBeamAttachment.Name = "BeamAttachment"playerBeamAttachment.Parent = humanoidRootPartlocal targetBeamAttachment = getTargetAttachment()playerBeam = beamTemplate:Clone()playerBeam.Attachment0 = playerBeamAttachmentplayerBeam.Attachment1 = targetBeamAttachmentplayerBeam.Enabled = trueplayerBeam.Parent = humanoidRootPartendlocal function setupPlayer()setupGoals()TutorialManager.setupPlayerProgress(player)goalIndex = player:WaitForChild("GoalProgress")player.CharacterAdded:Connect(createBeamForCharacter)if player.Character thencreateBeamForCharacter(player.Character)endendsetupPlayer()goalIndex.Changed:Connect(updateBeamTarget)播放項目以測試腳本。使用互動功能移動從一個攤位到另一個攤位,以查看代碼是否正常運作。
排除故障提示
問題 : 遊戲開始時會播放粒子。
前往伺服器儲存 > 教學粒子 > 爆炸。檢查啟用為關閉。 問題 : 譯譯器中的警告,例如「無限收集」。
因為腳本正在尋找特定位置的特定對象,所以有可能是一個部分的名稱不正確。雙檢查每個部分的名稱和位置是否與教學匹配。
腳本優點和限制
如果您在體驗中使用此教學系統,請記住以追蹤中事項: 好處
- 例如教學結束等事件可以用來觸發其他腳本。例個體、實例,當這個事件發生時,你可以獎勵玩家一個特殊物品。
- 教學粒子腳本可以一次播放多個粒子。您可以在伺服器儲存/教學參數中添加更多粒子來獲得更複雜的效果。 限制
- 教學中玩家的進度不會永久,這意味著你必須寫一些方法來保存那些進度。如需指導,請參閱文章:儲存資料。