ถึงเวลาที่จะรวบรวมงานทั้งหมดนี้แล้ว! ตอนนี้ที่คุณได้สร้างส่วนประกอบลําแสงและอนุภาคแล้ว คุณจะเพิ่มสคริปต์ที่สร้างไว้ล่วงหน้าสามรายการสคริปต์เหล่านี้จัดการบทแนะนำโดยบอกส่วนประกอบว่าจะทำอะไรเมื่อใดตัวอย่างเช่น สคริปต์จะสร้างลําแสงสําหรับผู้เล่นใหม่และปล่อยอนุภาคเมื่อพวกเขาโต้ตอบกับเป้าหมาย
เก็บลําแสงและอนุภาค
ก่อนที่จะเพิ่มสคริปต์ ลำแสงและอนุภาคจะต้องย้ายไปที่ที่สคริปต์จะสามารถสร้างสำเนาได้ตามต้องการ
ใน ReplicatedStorage สร้างโฟลเดอร์ใหม่ชื่อ PlayerTutorial ย้าย TutorialBeam ออกจาก TestPlayer และเข้าสู่โฟลเดอร์ใหม่
ใน พื้นที่เก็บของเซิร์ฟเวอร์ สร้างโฟลเดอร์ชื่อ TutorialParticles ย้ายอนุภาค ระเบิด ออกจาก TestPlayer ไปยังโฟลเดอร์นั้น
เมื่อลำแสงและตัวปล่อยอนุภาคถูกย้ายแล้ว คุณไม่ต้องใช้ TestPlayer อีกต่อไป ลบ ผู้เล่นทดสอบเพราะสคริปต์จะทำงานร่วมกับผู้เล่นจริงเมื่อเสร็จสิ้น
สร้างเหตุการณ์
ทุกครั้งที่ผู้เล่นโต้ตอบกับเป้าหมาย สคริปต์การสอนจะต้องรู้เพื่อที่จะอัปเดตความคืบหน้าของผู้เล่นนั้นและปล่อยผลกระทบของอนุภาคเพื่อแจ้งให้สคริปต์ทราบ สัญญาณสามารถส่งโดยใช้เหตุการณ์ ****
- ใน ReplicatedStorage > PlayerTutorial สร้างวัตถุ RemoteEvent สองวัตถุ ชื่อว่า NextGoal และ TutorialEnd
เพิ่มสคริปต์
สคริปต์สามต่อไปนี้จะค้นหาตัวปล่อยอนุภาคและวัตถุลําแสงที่สร้างไว้ก่อนหน้านี้และจัดการระบบการสอนใช้
ใน ReplicatedStorage > PlayerTutorial > สร้างโมดูลใหม่ ModuleScript ชื่อ 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สคริปต์นี้รันโค้ดสำหรับการจัดการความคืบหน้าของผู้เล่นในบทแนะนำการใช้งานซึ่งรวมถึงภารกิจเช่นการรันโค้ดเพื่อโต้ตอบกับเป้าหมายหรือสิ่งที่เกิดขึ้นเมื่อบทแนะนำสิ้นสุด
ใน ServerScriptService สร้าง สคริปต์ใหม่ ชื่อ 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 สร้าง สคริปต์ท้องถิ่น ใหม่ชื่อ TutorialScript
จากนั้นให้วางสคริปต์ด้านล่าง สคริปต์นี้จะสร้างและจัดการลําแสงที่ใช้เพื่อนําทางผู้เล่น
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)เล่นโครงการเพื่อทดสอบสคริปต์ ย้ายจากบูธไปยังบูธโดยใช้คุณลักษณะการโต้ตอบเพื่อดูว่าโค้ดทำงานได้หรือไม่
เคล็ดลับการแก้ปัญหา
ปัญหา : อนุภาคเล่นเมื่อเกมเริ่มต้น
ไปที่ ServerStorage > อนุภาคการสอน > ระเบิด ตรวจสอบให้ปิดใช้งาน ปัญหา : คําเตือนในคอมไพเลอร์เช่น "ผลผลิตไม่สิ้นสุด"
เนื่องจากสคริปต์กำลังมองหาวัตถุเฉพาะในสถานที่เฉพาะ จึงเป็นไปได้ว่าส่วนหนึ่งจะถูกเรียกชื่อผิดตรวจสอบให้แน่ใจว่าชื่อและตำแหน่งของแต่ละส่วนในเกมตรงกับบทแนะนำการใช้งาน
ประโยชน์และข้อจำกัดของสคริปต์
หากคุณใช้ระบบการสอนนี้ในประสบการณ์ของคุณ กำลังติดตาม: ประโยชน์
- อีเวนต์เช่น TutorialEnd สามารถใช้เพื่อเรียกใช้สคริปต์อื่นได้ อินสแตนซ์คุณสามารถมอบรางวัลให้กับผู้เล่นไอเทมพิเศษเมื่ออีเวนต์นี้เกิดขึ้น
- สคริปต์ TutorialParticles สามารถเล่นอนุภาคหลายอนุภาคได้พร้อมกันคุณสามารถเพิ่มอนุภาคเพิ่มเติมใน ServerStorage/TutorialParticles สําหรับผลกระทบที่ซับซ้อนมากขึ้น ข้อจํากัด
- ความคืบหน้าของผู้เล่นในบทแนะนำการใช้งานไม่ถาวรซึ่งหมายความว่าคุณจะต้องเขียนโค้ดบางวิธีในการบันทึกความคืบหน้านั้นสำหรับคำแนะนำดูบทความ: การบันทึกข้อมูล