Ad
Log in to vote
1

How would I make a monster spawner but the monsters can only walk in certain areas?

Asked by 8 days ago
Edited 8 days ago

I have made a part that spawns monsters but haven't made the script for it yet. The main bit is making the monsters not being able to travel in different stages in my game, I want them to have a restricted zone for where they can go. Can you help me?

1 answer

Log in to vote
0
Answered by 8 days ago
Edited 8 days ago

You can use PathfindingService. PathfindingService let’s your NPC to go from one destination to another without bumping into walls or falling down holes. You can read the introduction in the wiki, DevForum, or a youtube video.

Those are some introductions to Pathfinding. To make a full AI to your monster, I recommend making waypoints (basically destination parts that the NPC can go in the map) and following GnomeCode’s Teddy AI Tutorial because it helps.

And to respawn the NPC when it get’s stuck for amount of time (let’s say 10 seconds), we can get it’s original spawn position, and when it reaches it’s max retries, it respawns. So it should look like this:

local StuckRetries = 0
local MaxStuckRetries = 10
local teddy = script.Parent
local spawnPoint = teddy.HumanoidRootPart.CFrame

task.spawn(function()
   while true do
      local currentPos = teddy.HumanoidRootPart.Position
      task.wait(1)
      if (teddy.HumanoidRootPart.Position - currentPos).Magnitude < 1 then -- checks if the npc is staying in it’s position
         StuckRetries += 1 -- adds the retries
         if StuckRetries >= MaxStuckRetries then -- if the retries reached it’s limit
            teddy:PivotTo(spawnPoint) -- teleports the npc to spawn
            StuckRetries = 0 -- resets the retries
         end
      else
         StuckRetries = 0 -- resets if the npc unstucks itself
      end
   end
end
Ad

Answer this question