I forgot to say the nodes will be inside predetermined rooms since the map will br rsndomly generated, this means it has a certain ampunt of rooms but can be set up in a random order so there wont be a nodes folder
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course! That's a very common and practical scenario for procedurally generated maps. The system needs to be dynamic and discover its environment rather than relying on a fixed structure like a folder.
This updated script will first scan the entire workspace to find all valid "rooms" and then gather the "nodes" from within them. This makes it independent of the map's layout.
Step 1: Prepare Your Assets
Room Models: Your randomly generated map should consist of Models. Let's assume each room model has a unique identifier. A good way is to name them consistently, like Room_Corridor, Room_Loot, etc. For this script, we'll look for any model whose name starts with "Room".
Node Parts: Inside each room model, place one or more Parts named exactly "Node". These are the points the monster will travel between.
Monster Model: Make sure your monster is a Model with a Humanoid and a PrimaryPart set (usually HumanoidRootPart). This is required for MoveTo() to work correctly.
Here's a visual example of the hierarchy in the Explorer:
code
Workspace
- Room_Start (Model)
- Floor (Part)
- Walls (Model)
- Node (Part) <-- A navigation point
- Room_Corridor (Model)
- Floor (Part)
- Node (Part) <-- Another navigation point
- Room_End (Model)
- Node (Part) <-- And another
- Monster (Model)
- HumanoidRootPart (Part)
- Humanoid (Humanoid)
- MonsterScript (Script) <-- Place the script here
Step 2: The Monster Script
Place this script inside your monster Model.
lua
-- Monster Pathfinding Script
-- Place this script inside the monster model.
local monster = script.Parent
local humanoid = monster:WaitForChild("Humanoid")
local rootPart = monster:WaitForChild("HumanoidRootPart")
-- -- Configuration -- --
local ROOM_PREFIX = "Room" -- The script will find any model in Workspace whose name starts with this.
local NODE_NAME = "Node" -- The name of the node parts inside the rooms.
local ABILITY_CHANCE = 0.3 -- 30% chance to use an ability at a node.
local MOVE_TIMEOUT = 10 -- Max seconds to wait for a move to complete.
local DECISION_DELAY = 1 -- Seconds to wait at a node before moving again.
-- -- State Variables -- --
local allNodes = {}
local currentNode = nil
local previousNode = nil
-- --- Functions --- --
-- Scans the workspace to find all valid nodes within rooms.
function findAllNodes()
print("Monster is scanning for navigation nodes...")
-- Clear the existing list in case the map changes
table.clear(allNodes)
for _, model in ipairs(workspace:GetChildren()) do
-- Check if the object is a model and its name starts with the ROOM_PREFIX
if model:IsA("Model") and model.Name:sub(1, #ROOM_PREFIX) == ROOM_PREFIX then
-- Search for parts named NODE_NAME inside this room model
for _, child in ipairs(model:GetChildren()) do
if child:IsA("BasePart") and child.Name == NODE_NAME then
table.insert(allNodes, child)
end
end
end
end
print("Scan complete. Found " .. #allNodes .. " nodes.")
end
-- Finds the closest node to a given position, excluding a specific node.
function findClosestNode(position, excludeNode)
local closestNode = nil
local minDistance = math.huge
for _, node in ipairs(allNodes) do
-- The node must not be the one we want to exclude (the previous one)
if node ~= excludeNode then
local distance = (node.Position - position).magnitude
if distance < minDistance then
minDistance = distance
closestNode = node
end
end
end
return closestNode
end
-- Moves the monster to a target part's position.
function moveToTarget(targetPart)
if not targetPart then return end
print(monster.Name .. " is moving to " .. targetPart.Parent.Name .. "/" .. targetPart.Name)
humanoid:MoveTo(targetPart.Position)
-- Wait for the move to finish or time out
local success = humanoid.MoveToFinished:Wait(MOVE_TIMEOUT)
if not success then
warn(monster.Name .. " move timed out. Teleporting to target.")
-- As a fallback, teleport the monster if it gets stuck
rootPart.CFrame = targetPart.CFrame + Vector3.new(0, 3, 0)
end
end
-- --- Main Logic --- --
-- 1. Initial scan to populate the node list.
findAllNodes()
-- Check if any nodes were found. If not, the monster can't move.
if #allNodes == 0 then
warn("Monster could not find any nodes in the map. Pathfinding will not start.")
return -- Stop the script
end
-- 2. Find the absolute closest node to start with.
print("Finding starting node...")
currentNode = findClosestNode(rootPart.Position, nil) -- 'nil' means don't exclude any node yet
if currentNode then
moveToTarget(currentNode)
else
warn("Monster found nodes, but couldn't pathfind to a starting one.")
return
end
-- 3. Start the main pathfinding loop.
while true do
-- Wait at the current node for a moment
wait(DECISION_DELAY)
-- Randomly decide whether to leave an ability
if math.random() < ABILITY_CHANCE then
-- This is where you would trigger the ability. For now, it's a print statement.
print(monster.Name .. " left an ability at '" .. currentNode.Name .. "' in room '" .. currentNode.Parent.Name .. "'!")
end
-- Decide where to go next
print("Deciding next move...")
local nextNode = findClosestNode(currentNode.Position, previousNode)
if nextNode then
-- Update the state variables *before* moving
previousNode = currentNode
currentNode = nextNode
-- Move to the new target
moveToTarget(currentNode)
else
-- This can happen if there are only 2 nodes, it will just ping-pong.
-- If there's only 1 node, it will stop here.
warn(monster.Name .. " has nowhere else to go. Staying put.")
-- You could add logic here to make it wait longer or rescan for new nodes.
wait(5)
end
end
How to Use and Customize
Place the Script: Put the script directly inside the monster's Model. The script will automatically find the Humanoid and HumanoidRootPart.
Adjust Configuration:
ROOM_PREFIX: If your rooms are named differently (e.g., "Dungeon_Section1"), change this to "Dungeon_Section".
NODE_NAME: If you prefer to call your nodes something else (like "PatrolPoint"), change this value.
ABILITY_CHANCE: Change this value from 0.0 (0%) to 1.0 (100%) to control how often the ability is used. 0.3 is a 30% chance.
Dynamic Maps: If your map generates rooms after the game has started, you could call findAllNodes() periodically or connect it to an event that fires when a new room is created. For most setups, running it once at the start is sufficient.