Day 2 of asking help for making NPCs not spawning in water

The damn cows spawn in the water all the time, and I want that to change

-- 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(200, 200), 5, math.random(200, 200))

	-- 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()

U can actually use an attribute and check whether that attribute exists or not.

(THE Part itself

Could you show me how it’ll work?

There is actually a lot of methods to solve this

Put a if and else in your clone script

If part(The water one)

There are also tutorial about attributes on youtube go and watcht them

If the water is actually a terrain…

It would bd impossible.

This is terrain, so yea… how would I do this?

First off your math.random is between 200 and 200 so it’ll always be 200.
Also, you could send a raycast down to see if it hits water and if it does recalculate the position.

local function getRandomPosition()
	local randomPosition = Vector3.new(math.random(0, 200), 5, math.random(0, 200))
	
	local raycastParams = RaycastParams.new()
	raycastParams.IgnoreWater = false
	
	local raycastResult = workspace:Raycast(randomPosition, Vector3.new(0, -1000, 0))
	
	if raycastResult then
		if raycastResult.Material == Enum.Material.Water then
			return getRandomPosition()
		else
			return randomPosition
		end
	else
		return randomPosition
	end
end

local function respawnNPC()
	local randomPosition = getRandomPosition()

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

Here’s how you could do that.

Helo hello,terrain doe not have any position.

There is one solution.Just change the math.radnom position and ensure that it didnt hit the water.

i’m not checking it’s position?

That didn’t work, The npc dont spown

Follow the other person code. See whether can work or not

Does it show up in the workspace at all? Could just be spawning out of bounds.

Didnt show in the workspace at all

U can also make an invisible part .and i think that will work???

What do you mean by an invisible part?

Like create a new part,put the part on top of the terrain u want the cow to spawn in.Make sure these properties are there

ANCHORED = TRUD
CANCOLLIDE = FALSE

Are there any errors? Also try printing out the random position to make sure it’s on the map.

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

Oh yeah also SetPrimaryPartCFrame is deprecated.so use this instead CFrame = another CFrame

Oh right, use :MoveTo instead. Also do that after you parent it.

local function respawnNPC()
	local randomPosition = getRandomPosition()
	
	-- Move the NPC to the random position
	local newNPC: Model = game.ServerStorage:WaitForChild("NPC"):Clone()
	newNPC.Parent = workspace
	newNPC:MoveTo(randomPosition)
end