How to make the Npcs not spawn in water

-- Configuration
local respawnIntervalMin = 10  -- Minimum respawn interval in seconds
local respawnIntervalMax = 30  -- Maximum respawn interval in seconds

-- Function to respawn an NPC at a random location
local function respawnNPC()
	-- Get a random position within the workspace
	local randomPosition = Vector3.new(math.random(-50, 50), 5, math.random(-50, 50))

	-- Move the NPC to the random position
	local newNPC = game.ServerStorage.NPC:Clone()
	newNPC:SetPrimaryPartCFrame(CFrame.new(randomPosition))
	newNPC.Parent = workspace
end

-- Function to create and respawn NPCs at random intervals
local function startRandomNPCRespawns()
	while true do
		wait(math.random(respawnIntervalMin, respawnIntervalMax))
		respawnNPC()
	end
end

-- Start the coroutine to respawn NPCs at random intervals
startRandomNPCRespawns()
2 Likes

You could make a range of positions where the NPCs cannot spawn and check if the RandomPosition falls within the bounds.

2 Likes

How would I do that, show me an example

1 Like

You first need to understand a few things about Positions and Sizes. A Part’s position is the position of the center of the part, you can test this out by parenting an attachment to a part and watching both have the same position regardless of size.
An example:
Lets say the water is position at 0,0,0 and has a size of 50,1,50. That means the water stretches half the X value to the negative and positive directions, same thing with Y and Z. (Add the half values to the position.)

The Positions the water is within are:
25,-0.5, 25 to -25,05,-25 so you could do something like this:

local RandomPosition : Vector3
local X = RandomPosition.X
local Y = RandomPosition.Y
local Z = RandomPosition.Z

if X <= 25 and X >= -25 then 
   if Y <= 0.5 and Y>= 0.5 then 
     if Z <= 25 and Z >= 25 then 
         -- Position is within range which is a bad thing
     end 
   end
end

Another example with a different position:
Size = 50,1,50
Position = 130, 20, 40

Range = 155, 20.5, 65 to 105, 19.5, 15

Hopefully that makes sense.