Code Generator | Lua

Roblox NPC Training Automation Script

This Lua script facilitates NPC training in Roblox by managing player interactions, NPC movements, and training progression with structured code and comprehensive documentation. It enhances engagement through communication and movement


Empty image or helper icon

Prompt

--// Made by MademoiselleMadelynn
--// NPC Training, 12/08/24, Started, 18:50.
--// Last Updated: 18:51, Updated By: Madelynn

----// A

local HSCGuiModule = require(game:GetService("StarterPlayer").StarterPlayerScripts.Modules.GuiModules.HSCGui)
local NPCMovementModule = require(game:GetService("ReplicatedStorage").ModuleScript)
local NPC = workspace.NPC.NPC
local NPC2 = workspace.NPC.NPC2
local FirstTraining = workspace.NPC.Scripting.Positions.Toggle.ClickDetector

local Messages = {
    ["First"] = "Ah! Welcome to your training, before we start let me go over the basics."
}

----// B


FirstTraining.MouseClick:Connect(function(plr)
    NPCMovementModule.ChatNPC(NPC, plr.Name .. " has started the first part of the training.")
    FirstTraining:Destroy()

    local positions = {
        Vector3.new(-134.275, -30.936, -540.049),   -- Position One
        Vector3.new(-139.54, -30.936, -548.314),    -- Position Two
        Vector3.new(-145.117, -30.936, -549.623),   -- Position Three
        Vector3.new(-158.603, -30.936, -540.083),   -- Position Four
        Vector3.new(-161.736, -33.939, -514.582),   -- Position Five
        Vector3.new(-190.364, -33.939, -463.856),   -- Position Six
        Vector3.new(-168.647, -20.669, -429.345),   -- Position Seven
        Vector3.new(-165.05, -13.169, -406.872),    -- Position Eight
        Vector3.new(-176.413, -10.169, -399.903),   -- Position Nine
        Vector3.new(-222.642, -1.919, -455.89),     -- Position Ten
        Vector3.new(-242.194, 1.831, -461.338),     -- Position Eleven
        Vector3.new(-203.886, 19.831, -418.788),    -- Position Twelve
        Vector3.new(-191.324, 26.831, -398.595),    -- Position Thirteen
        Vector3.new(-216.597, 30.3, -382.748),    -- Position Fourteen
        NPCMovementModule.Rotate(NPC, CFrame.Angles(math.rad(45), 0, 0))
    }

    local positionsNPC2Extra = {
        Vector3.new(-223.7, 30.3, -380.7),
    }

    NPCMovementModule.StartTraining(NPC, positions)

    NPCMovementModule.StartTraining(NPC2, { unpack(positions, 1, #positions - 1) }, function()
        NPCMovementModule.StartTraining(NPC2, positionsNPC2Extra)
    end)
end)

Answer

Lua NPC Training Script

This script defines the behavior for NPC training in a Roblox game. It handles player interactions, NPC movement, and training progression. Below is an improved version of the original script with structured code, inline comments, and comprehensive documentation.

Imports and Initializations

-- Made by MademoiselleMadelynn
-- NPC Training, 12/08/24, Started, 18:50.
-- Last Updated: 18:51, Updated By: Madelynn

-- Importing necessary modules
local HSCGuiModule = require(game:GetService("StarterPlayer").StarterPlayerScripts.Modules.GuiModules.HSCGui)
local NPCMovementModule = require(game:GetService("ReplicatedStorage").ModuleScript)
local NPC = workspace.NPC.NPC
local NPC2 = workspace.NPC.NPC2
local FirstTraining = workspace.NPC.Scripting.Positions.Toggle.ClickDetector

-- Define messages for NPC communications
local Messages = {
    ["First"] = "Ah! Welcome to your training, before we start let me go over the basics."
}

Main Functionality

-- Setup Click Detector for the First Training Interaction
FirstTraining.MouseClick:Connect(function(plr)
    -- Notify that the player has started training
    NPCMovementModule.ChatNPC(NPC, plr.Name .. " has started the first part of the training.")
    
    -- Remove the ClickDetector to prevent re-triggering
    FirstTraining:Destroy()

    -- Define the positions for NPC movement during training
    local positions = {
        Vector3.new(-134.275, -30.936, -540.049),  -- Position One
        Vector3.new(-139.54, -30.936, -548.314),   -- Position Two
        Vector3.new(-145.117, -30.936, -549.623),  -- Position Three
        Vector3.new(-158.603, -30.936, -540.083),  -- Position Four
        Vector3.new(-161.736, -33.939, -514.582),  -- Position Five
        Vector3.new(-190.364, -33.939, -463.856),  -- Position Six
        Vector3.new(-168.647, -20.669, -429.345),  -- Position Seven
        Vector3.new(-165.05, -13.169, -406.872),   -- Position Eight
        Vector3.new(-176.413, -10.169, -399.903),  -- Position Nine
        Vector3.new(-222.642, -1.919, -455.89),    -- Position Ten
        Vector3.new(-242.194, 1.831, -461.338),    -- Position Eleven
        Vector3.new(-203.886, 19.831, -418.788),   -- Position Twelve
        Vector3.new(-191.324, 26.831, -398.595),   -- Position Thirteen
        Vector3.new(-216.597, 30.3, -382.748),     -- Position Fourteen
    }
    
    -- Rotate the NPC to face the designated direction
    NPCMovementModule.Rotate(NPC, CFrame.Angles(math.rad(45), 0, 0))

    -- Extra position for NPC2
    local positionsNPC2Extra = {
        Vector3.new(-223.7, 30.3, -380.7),
    }

    -- Start training movement for both NPCs
    NPCMovementModule.StartTraining(NPC, positions)
    NPCMovementModule.StartTraining(NPC2, { unpack(positions, 1, #positions - 1) }, function()
        NPCMovementModule.StartTraining(NPC2, positionsNPC2Extra)
    end)
end)

Key Features

  1. NPC Interaction: Sets up a mouse click event on the ClickDetector that begins the training when clicked by a player.
  2. Movement Control: Utilizes the NPCMovementModule to control NPC movements across a predefined series of positions.
  3. Communication: NPCs inform players of their progress through message prompts.
  4. Input Validation: Implicit validation is included through the use of player interactions (the ClickDetector won't trigger for anything other than players).

Usage Example

This script is to be included in a ModuleScript within the Roblox studio environment. The NPCs defined should be placed within the workspace, ensuring that the respective ClickDetector exists where players can interact with it.

  • Set up the NPCs and ClickDetector in the Roblox workspace and attach this script to ensure it's executed when players interact with the designated object.
  • When the player clicks the training start button, the NPCs will begin a series of movements, aiding game immersion and player engagement.

Conclusion

This script exemplifies best practices in game scripting through structured code, clear documentation, and robust logic for NPC interactions. For further enhancement of scripting and game development skills, consider courses available on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This Lua script facilitates NPC training in Roblox by managing player interactions, NPC movements, and training progression with structured code and comprehensive documentation. It enhances engagement through communication and movement control.