Certainly! Below is a simple Lua script for Roblox that moves an NPC forward by 220 studs. This script assumes that the NPC is a Model with a Humanoid and a PrimaryPart (usually the HumanoidRootPart).
You can insert this script into a ServerScript inside the NPC model or in a ServerScriptService.
-- Get the NPC model
local npc = script.Parent
-- Ensure the NPC has a PrimaryPart
if not npc:FindFirstChild("HumanoidRootPart") then
warn("NPC does not have a HumanoidRootPart!")
return
end
local primaryPart = npc.HumanoidRootPart
-- Distance to move
local distance = 220
-- Get the current position and look vector
local currentPosition = primaryPart.Position
local lookVector = primaryPart.CFrame.LookVector
-- Calculate the new position
local newPosition = currentPosition + lookVector * distance
-- Move the NPC
primaryPart.CFrame = CFrame.new(newPosition, primaryPart.CFrame.LookVector)
-- Alternatively if you want to move the NPC smoothly over time
-- Uncomment the below section and comment out the direct CFrame assignment above
-- local moveDuration = 2 -- Duration of the movement in seconds
-- local startTime = tick()
-- game:GetService("RunService").Stepped:Connect(function()
-- local currentTime = tick()
-- local elapsedTime = currentTime - startTime
-- if elapsedTime < moveDuration then
-- local t = elapsedTime / moveDuration
-- local interpolatedPosition = currentPosition:Lerp(newPosition, t)
-- primaryPart.CFrame = CFrame.new(interpolatedPosition, primaryPart.CFrame.LookVector)
-- else
-- primaryPart.CFrame = CFrame.new(newPosition, primaryPart.CFrame.LookVector)
-- end
-- end)
Explanation:
-
Get the NPC model:
local npc = script.Parent assumes the script is a child of the NPC model. If the script is located elsewhere, you will need to get a reference to the NPC differently.
-
Ensure the NPC has a PrimaryPart:
- The script checks if the NPC has a
HumanoidRootPart. If not it, warns and exits.
local primaryPart = npc.HumanoidRootPart gets the HumanoidRootPart, which is typically the PrimaryPart of the NPC.
-
Calculate the new position:
local lookVector = primaryPart.CFrame.LookVector gets the direction the NPC is facing.
local newPosition = currentPosition + lookVector * distance calculates the new position by moving distance studs in the direction the NPC is facing.
-
Move the NPC:
primaryPart.CFrame = CFrame.new(newPosition, primaryPart.CFrame.LookVector) instantly moves the NPC to the new position while maintaining its orientation.
-
Smooth Movement (Optional):
- The commented-out section provides an alternative to move the NPC smoothly over a specified duration (
moveDuration).
game:GetService("RunService").Stepped:Connect runs the movement logic every frame.
local interpolatedPosition = currentPosition:Lerp(newPosition, t) interpolates between the current and new positions based on the elapsed time.
- It will smoothly transition the NPC to the new position over the specified duration.
Choose either the instant movement or the smooth movement by commenting/uncommenting the appropriate sections.