Prompt
Answer
NPC Movement Module in Lua
This module provides functions for controlling NPC (Non-Playable Character) movement, rotation, chatting, and training sequences in a Roblox environment. Each function is well-commented to aid understanding and usage.
Code Implementation
local NPCMovementModule = {}
--- Move the NPC to a specified position.
-- @param NPC: The NPC object to be moved.
-- @param Position: The target Vector3 position to move the NPC to.
-- @return nil
-- @raises Warn if NPC has no Humanoid or if the Position is invalid.
function NPCMovementModule.Move(NPC, Position)
local humanoid = NPC:FindFirstChildOfClass("Humanoid")
-- Ensure the NPC has a Humanoid
if not humanoid then
warn("No Humanoid found in NPC.")
return
end
-- Ensure Position is valid
if not Position or typeof(Position) ~= "Vector3" then
warn("Invalid position provided. It must be a Vector3.")
return
end
-- Move to the target position
humanoid:MoveTo(Position)
humanoid.MoveToFinished:Wait() -- Wait for the NPC to reach the position
end
--- Rotate the NPC to face a specified rotation.
-- @param NPC: The NPC object to be rotated.
-- @param Rotation: The target CFrame or angle to rotate the NPC to.
-- @return nil
-- @raises Warn if NPC has no Humanoid.
function NPCMovementModule.Rotate(NPC, Rotation)
local humanoid = NPC:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.RootPart then
humanoid.RootPart.CFrame = CFrame.new(humanoid.RootPart.Position, Rotation)
else
warn("No Humanoid found in NPC to rotate.")
end
end
--- Make the NPC chat with a specific message.
-- @param NPC: The NPC object that will chat.
-- @param Msg: The message the NPC will say.
-- @return nil
-- @raises Warn if NPC does not have a Head to chat from.
function NPCMovementModule.ChatNPC(NPC, Msg)
local Chat = game:GetService("Chat")
local head = NPC:FindFirstChild("Head")
if head then
Chat:Chat(head, Msg, Enum.ChatColor.White)
else
warn("NPC does not have a Head.")
end
end
--- Start a training sequence for the NPC moving to multiple positions.
-- @param NPC: The NPC object that will be trained.
-- @param positions: A table of Vector3 positions for training.
-- @param onComplete: A callback function that is called after training is complete.
-- @return nil
function NPCMovementModule.StartTraining(NPC, positions, onComplete)
-- Start a coroutine for the NPC's movement
coroutine.wrap(function()
for i, pos in ipairs(positions) do
NPCMovementModule.Move(NPC, pos)
task.wait(0.3) -- Wait briefly between movements
end
-- After all positions are reached, call the onComplete callback
if onComplete then
onComplete()
end
end)()
end
return NPCMovementModule
Key Features
- Move Function: Handles NPC movement to specified positions with validation on humanoid and position type.
- Rotate Function: Aligns NPC's rotation to face a given point when specified.
- Chat Function: Allows NPCs to chat by validating the presence of a head object.
- Start Training Function: Initiates a sequence that moves the NPC through a series of positions with an optional callback upon completion.
Usage Example
Here's how you might use the NPCMovementModule
in a Roblox script:
local NPCMovementModule = require(script.NPCMovementModule)
local NPC = game.Workspace.NPC -- Assume NPC is placed in the Workspace
local trainingPositions = {
Vector3.new(0, 0, 0),
Vector3.new(10, 0, 0),
Vector3.new(20, 0, 0)
}
-- Move NPC, chat, and start training
NPCMovementModule.ChatNPC(NPC, "Let's start training!")
NPCMovementModule.StartTraining(NPC, trainingPositions, function()
NPCMovementModule.ChatNPC(NPC, "Training complete!")
end)
Conclusion
This NPCMovementModule provides a structured way to manage NPC behaviors in Roblox. Each function includes input validation and descriptive warnings to improve debugging and usability. For more advanced programming techniques, consider exploring courses on the Enterprise DNA Platform.
Description
This Lua module facilitates NPC movement, rotation, chatting, and training sequences in Roblox, featuring validation, well-defined functions, and callback mechanisms for effective NPC behavior management.