Aggiungi script

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

È ora di mettere tutto questo lavoro insieme! Ora che hai creato i componenti del fascio e delle particelle, aggiungerai tre script predefiniti.Questi script gestiscono il tutorial dicendo ai componenti quando fare cosa.Ad esempio, gli script creeranno fascie per i nuovi giocatori e emetteranno particelle ogni volta che interagiscono con i goal.

Magazzina fascio e particelle

Prima di aggiungere gli script, il fascio e le particelle devono essere spostati dove gli script saranno in grado di fare copie di essi come necessario.

  1. In ReplicatedStorage , crea una nuova cartella chiamata PlayerTutorial . Sposta TutorialBeam fuori da TestPlayer e nella nuova cartella.

  2. In ServerStorage , crea una cartella chiamata TutorialParticles .Sposta la particella Esplosione fuori da TestPlayer in quella cartella.

  3. Una volta spostati il fascio e l'emittente di particelle, non hai più bisogno del TestPlayer. Elimina TestPlayer poiché lo script funzionerà con giocatori reali quando terminato.

Crea eventi

Ogni volta che i giocatori interagiscono con un obiettivo, lo script del tutorial deve sapere così da poter aggiornare i progressi di quel Giocatoree emettere l'effetto delle particelle.Per informare gli script, i segnali possono essere inviati utilizzando eventi .

  1. In ReplicatedStorage > PlayerTutorial, crea due oggetti RemoteEvent . Nominali NextGoal e TutorialEnd .

Aggiungi gli script

I tre script seguenti cercheranno il mittente delle particelle e gli oggetti del raggio creati in precedenza e gestiranno il sistema di tutorial.

  1. In ReplicatedStorage > PlayerTutorial > crea un nuovo ModuleScript chiamato TutorialManager .

    Sostituisci il codice predefinito copiando e incollando l'intero codice qui sotto.


    local TutorialManager = {}
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local tutorialFolder = ReplicatedStorage:WaitForChild("PlayerTutorial")
    local TutorialEndEvent = tutorialFolder:WaitForChild("TutorialEnd")
    local NextGoalEvent = tutorialFolder:WaitForChild("NextGoal")
    -- Le parti dell'obiettivo devono essere ordinate nella tabella, altrimenti l'ordine dell'obiettivo potrebbe essere diverso in gioco
    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")
    -- Segno di posizione per ulteriori codice. E.g. se vuoi inviare messaggi al server per eseguire altre attività
    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
    -- Aumenta il tracciatore degli obiettivi del Giocatore
    local currentGoalIndex = player:WaitForChild("GoalProgress")
    currentGoalIndex.Value += 1
    end
    end
    -- Crea un valore int per tracciare localmente i progressi del Giocatoreattraverso gli obiettivi del tutorial
    function TutorialManager.setupPlayerProgress(player)
    local currentGoalProgress = Instance.new("IntValue")
    currentGoalProgress.Name = "GoalProgress"
    currentGoalProgress.Value = 1
    currentGoalProgress.Parent = player
    end
    return TutorialManager

    Questo script esegue il codice per la gestione del progresso di un Giocatorenel Tutoriale.Questo include compiti come l'esecuzione del codice per interagire con gli obiettivi, o cosa succede quando il tutorial è finito.

  2. In ServerScriptService , crea un nuovo Script chiamato TutorialParticles .

    Incolla il codice qui sotto.


    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")
    -- Passa attraverso le particelle sull'allegato e riproducili secondo il tipo di particella
    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
    -- Clone le particelle nella cartella, anche se ci sono più di una e attaccarle al Giocatore
    for _, emitter in ServerStorage.TutorialParticles:GetChildren() do
    emitter:Clone().Parent = playerParticleAttachment
    end
    end)
    end
    Players.PlayerAdded:Connect(setupPlayerParticles)
    NextGoalEvent.OnServerEvent:Connect(playParticleBurst)

    Questo script riproduce la particella di esplosione ogni volta che i giocatori interagiscono con i goal.C'è anche una variabile chiamata EMIT_RATE che determina quante particelle si generano durante un'interazione.

  3. In StarterPlayer > StarterPlayerScripts, crea un nuovo LocalScript chiamato TutorialScript .

    Quindi, incolla lo script seguente. Questo script crea e gestisce il fascio utilizzato per guidare i giocatori.


    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. Gioca al progetto per testare gli script. Passa da cabina a cabina, usando la funzione interattiva per vedere se il codice funziona.

Suggerimenti per la risoluzione dei problemi

Problema : le particelle giocano quando inizia il gioco.

  • Vai in ServerStorage > Particelle tutorial > Esplosione. Controlla Disabilitato per essere Off. Problema : Avvertimenti nel compilatore come un "rendimento infinito".

  • Poiché lo script sta cercando oggetti specifici in determinati luoghi, è possibile che una parte sia nominata erroneamente.Controlla due volte che il nome e la posizione di ciascuna parte in gioco corrispondono al Tutoriale.

Vantaggi e limitazioni dello script

Se stai usando questo sistema di tutorial nella tua esperienza, tieni presente quanto Seguendo: Vantaggi

  • Gli eventi come TutorialEnd possono essere utilizzati per attivare altri script. Ad esempio, puoi assegnare ai giocatori un oggetto speciale quando questo evento si attiva.
  • Lo script TutorialParticles può riprodurre più particelle contemporaneamente.Puoi aggiungere più particelle in ServerStorage/TutorialParticles per effetti più complessi. Limitazioni
  • Il progresso del giocatore nel tutorial non è persistente, il che significa che dovrai codificare in qualche modo il salvataggio di quel progresso.Per una guida, vedi l'articolo: Salvataggio dei dati.