make me a roblox script. i want to have some coordinates and the npc has to pick a random one out of them and move there.
Assistant
Certainly! Here is a basic script for a Roblox NPC that picks a random coordinate and moves there. This script assumes you have the NPC as a model with a Humanoid and HumanoidRootPart.
Firstly, ensure you have a model of your NPC in the Workspace with the mentioned parts. Also, the NPC should have a Script inside it.
Here’s the script for the NPC to pick a random coordinate and move there:
lua
-- Define your coordinates here
local coordinates = {
Vector3.new(0, 0, 0),
Vector3.new(10, 0, 10),
Vector3.new(-10, 0, -10),
Vector3.new(20, 0, 20)
}
-- Get references to necessary parts
local npc = script.Parent
local humanoid = npc:FindFirstChild("Humanoid")
local humRootPart = npc:FindFirstChild("HumanoidRootPart")
-- Make sure we have essential parts before proceeding
if not humanoid or not humRootPart then
error("NPC missing Humanoid and/or HumanoidRootPart")
return
end
-- Function to move NPC to a specified destination
local function moveTo(destination)
humanoid:MoveTo(destination)
humanoid.MoveToFinished:Wait()
end
-- Pick a random coordinate and move there
local function pickRandomCoordinateAndMove()
local randomIndex = math.random(1, #coordinates)
local destination = coordinates[randomIndex]
moveTo(destination)
end
-- Main loop to pick a random coordinate and move there continuously
while true do
pickRandomCoordinateAndMove()
wait(2) -- Pause before picking the next coordinate
end
Explanation:
Coordinate Setup: You define a table coordinates that holds possible destinations for the NPC.
References: The script retrieves references to the necessary parts of the NPC (Humanoid and HumanoidRootPart).
Safety Check: Ensures that the essential parts are present before proceeding.
Movement Function: The moveTo function is used to command the NPC to walk to a given destination and wait until it finishes moving.
Random Selection: The pickRandomCoordinateAndMove function selects a random coordinate from the list and moves the NPC there.
Loop: The while loop keeps the NPC picking new coordinates and moving indefinitely, with a small pause (wait(2)) between moves. Adjust the delay based on how quickly you want the NPC to move to a new coordinate.
Implementation:
Add this script to your NPC model in the Roblox Studio.
Customize the coordinates to fit your game’s environment.
Run the game, and the NPC should move to random coordinates from your defined list.
Be sure to test and adjust the coordinates and script as per your game requirements!