Il est temps de rassembler tout ce travail ! Maintenant que vous avez créé les composants de rayon et de particule, vous ajouterez trois scripts prédéfinis.Ces scripts gèrent le tutoriel en indiquant aux composants quand faire quoi.Par exemple, les scripts créeront des rayons pour de nouveaux joueurs et émettront des particules chaque fois qu'ils interagissent avec des buts.
Stocker les rayons et les particules
Avant d'ajouter les scripts, le rayon et les particules doivent être déplacés là où les scripts pourront en faire des copies comme nécessaire.
Dans ReplicatedStorage , créez un nouveau dossier nommé PlayerTutorial . Déplacez le TutorialBeam hors de TestPlayer et dans le nouveau dossier.
Dans ServerStorage , créez un dossier nommé TutorialParticles .Déplacez la particule éclat hors de TestPlayer dans ce dossier.
Une fois que le faisceau et l'émetteur de particules sont déplacés, vous n'avez plus besoin du TestPlayer. Supprimer TestPlayer puisque le script fonctionnera avec des joueurs réels lorsqu'il sera terminé.
Créer des événements
Chaque fois que les joueurs interagissent avec un but, le script du tutoriel devra le savoir pour mettre à jour le progrès de ce joueur et émettre l'effet de particule.Pour informer les scripts, les signaux peuvent être envoyés en utilisant événements .
Dans ReplicatedStorage > PlayerTutorial, créez deux objets Événement à distance . Nommez-les prochain objectif et fin du tutoriel .
Ajoutez les scripts
Les trois scripts ci-dessous rechercheront les émetteurs de particules et les objets de rayon créés plus tôt et géreront le système de tutoriel.
Dans ReplicatedStorage > PlayerTutorial > créez un nouveau ModuleScript nommé TutorialManager .
Remplacez le code par défaut en copiant et en collant l'ensemble du code ci-dessous.
local TutorialManager = {}local ReplicatedStorage = game:GetService("ReplicatedStorage")local tutorialFolder = ReplicatedStorage:WaitForChild("PlayerTutorial")local TutorialEndEvent = tutorialFolder:WaitForChild("TutorialEnd")local NextGoalEvent = tutorialFolder:WaitForChild("NextGoal")-- Les parties de l'objectif doivent être commandées dans la table, sinon l'ordre de l'objectif peut être différent en jeulocal 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")-- Espace réservé pour un code ultérieur. Par exemple, si vous vouliez envoyer des messages au serveur pour effectuer d'autres tâchesendfunction TutorialManager.interactGoal(player)NextGoalEvent:FireServer()endfunction TutorialManager.getTutorialGoals()return goalPartsendfunction TutorialManager.nextGoal(player, goalParts)if checkTutorialEnd(player, goalParts) thenfinishTutorial(player)else-- Augmenter le tracker de but du joueurlocal currentGoalIndex = player:WaitForChild("GoalProgress")currentGoalIndex.Value += 1endend-- Crée une valeur int pour suivre localement la progression du joueur à travers les objectifs du tutorielfunction TutorialManager.setupPlayerProgress(player)local currentGoalProgress = Instance.new("IntValue")currentGoalProgress.Name = "GoalProgress"currentGoalProgress.Value = 1currentGoalProgress.Parent = playerendreturn TutorialManagerCe script exécute du code pour gérer la progression d'un joueur dans le tutoriel.Cela inclut des tâches comme l'exécution du code pour interagir avec les objectifs, ou ce qui se produit lorsque le tutoriel est terminé.
Dans ServerScriptService , créez un nouveau script nommé TutorialParticles .
Collez le code ci-dessous.
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")-- Traversez les particules sur l'attachement et jouez-les selon le type de particulefor _, 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-- Cloner des particules dans le dossier, même s'il y en a plus d'une et attacher au joueurfor _, emitter in ServerStorage.TutorialParticles:GetChildren() doemitter:Clone().Parent = playerParticleAttachmentendend)endPlayers.PlayerAdded:Connect(setupPlayerParticles)NextGoalEvent.OnServerEvent:Connect(playParticleBurst)Ce script joue la particule d'explosion chaque fois que les joueurs interagissent avec les buts.Il y a aussi une variable nommée EMIT_RATE qui détermine le nombre de particules générées pendant une interaction.
Dans StarterPlayer > StarterPlayerScripts, créez un nouveau LocalScript nommé TutorialScript .
Ensuite, collez le script ci-dessous. Ce script crée et gère le rayon utilisé pour guider les joueurs.
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)Jouez le projet pour tester les scripts. Déplacez-vous d'un stand à l'autre, en utilisant la fonction d'interaction pour voir si le code fonctionne.
Conseils de dépannage
Problème : les particules jouent lorsque le jeu commence.
Allez dans ServerStorage > Tutorial Particles > Burst. Vérifiez que Désactivé est off. Problème : Avertissements dans le compilateur tels qu'un "rendement infini".
Puisque le script recherche des objets spécifiques dans certains endroits, il est possible qu'une partie soit nommée incorrectement.Vérifiez que le nom et l'emplacement de chaque partie dans le jeu correspondent au tutoriel.
Avantages et limites des scripts
Si vous utilisez ce système de tutoriel dans votre expérience, gardez à l'esprit ce qui suivre: Avantages
- Des événements tels que TutorialEnd peuvent être utilisés pour déclencher d'autres scripts. Par instance, vous pouvez attribuer aux joueurs un objet spécial lorsque cet événement se déclenche.
- Le script TutorialParticles peut jouer plusieurs particules à la fois.Vous pouvez ajouter plus de particules dans ServerStorage/TutorialParticles pour des effets plus complexes. Limites
- La progression du joueur dans le tutoriel n'est pas persistante, ce qui signifie que vous devrez coder une façon de sauvegarder cette progression.Pour obtenir des conseils, voir l'article : Enregistrement des données.