Npcs Spawn Out Of Nowhere Instead Of Spawning On The Terrain

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    To Make The Script Spawn The Npcs On The Terrain And Not In The Middle Of Nowhere

  2. What is the issue? Include screenshots / videos if possible!
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve done everything I can, nothing works
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local npcModel = game.ServerStorage.noob

local function cloneNPC()
	local terrain = workspace.Terrain
	local terrainSize = terrain.Size
	local posX = math.random(0, terrainSize.X)
	local posZ = math.random(0, terrainSize.Z)
	local posY = math.random(0, terrainSize.Y)
	local cellX = math.floor(posX / 4)
	local cellZ = math.floor(posZ / 4)
	local cellY = math.floor(posY / 4)
	local spawnPosition = Vector3.new(posX, posY, posZ)

	local npcClone = npcModel:Clone()
	npcClone.Parent = workspace
	npcClone:PivotTo(CFrame.new(spawnPosition))
end

while true do
	wait(1)
	cloneNPC()
end

All i ask is for a quick fix to this script, cause idk what to do

this might be the silliest problem i’ve ever saw. so you’re getting a random position vector using random from 0 to the terrain size, so logically the position must be over positive. i assume your terrain position isn’t exactly Vector3.zero so the npcs falls into void. also i assume you’re using math.floor and then forgot to multiply the return value by 4 after dividing it by 4

this might work

local npcModel = game.ServerStorage.noob

local function cloneNPC()
	local terrain = workspace.Terrain
	local terrainSize = terrain.Size
	local posX = math.random(0, terrainSize.X) - (terrainSize.X/2)
	local posZ = math.random(0, terrainSize.Z) - (terrainSize.Z/2)
	local posY = math.random(0, terrainSize.Y)
	local cellX = math.floor(posX / 4) * 4
	local cellZ = math.floor(posZ / 4) * 4
	local cellY = math.floor(posY / 4) * 4
	local spawnPosition = terrain.Position + Vector3.new(0,terrainSize.Y/2,0) + Vector3.new(posX, posY, posZ) -- adding the half of the terrain size so the npc spawns on the top and not on the core.

	local npcClone = npcModel:Clone()
	npcClone.Parent = workspace
	npcClone:PivotTo(CFrame.new(spawnPosition))
end

while true do
	wait(1)
	cloneNPC()
end

ill try your script in just a second

i tested it, i set the gravity to 0 and for some reason, i cant even view their parts, they go into the void so fast

what does it look like


image
i added a part to the npc to see where they spawned and uhh, yeah, this is only 1 spawned in noob, the others idk where they spawned but i think somewhere similar

copy paste the script i sent above again.

local npcModel = game.ServerStorage.noob

local function cloneNPC()
	local terrain = workspace.Terrain
	local terrainSize = terrain.Size
	local posX = math.random(0, terrainSize.X) - (terrainSize.X/2)
	local posZ = math.random(0, terrainSize.Z) - (terrainSize.Z/2)
	local posY = math.random(0, terrainSize.Y)
	local cellX = math.floor(posX / 4) * 4
	local cellZ = math.floor(posZ / 4) * 4
	local cellY = math.floor(posY / 4) * 4
	local spawnPosition = terrain.Position + Vector3.new(0,terrainSize.Y/2,0) + Vector3.new(posX, posY, posZ) -- adding the half of the terrain size so the npc spawns on the top and not on the core.

	local npcClone = npcModel:Clone()
	npcClone.Parent = workspace
	npcClone:PivotTo(CFrame.new(spawnPosition))
end

while true do
	wait(1)
	cloneNPC()
end

no no no i mean copy paste it to your roblox studio script

well i did do that but to double check sure

image
those are parts to double check where they exactly spawned in

local npcModel = game.ServerStorage.noob

local function cloneNPC()
	local terrain = workspace.Terrain
	local terrainSize = terrain.Size
	local posX = math.random(0, terrainSize.X) - (terrainSize.X/2)
	local posZ = math.random(0, terrainSize.Z) - (terrainSize.Z/2)
	local posY = math.random(0, terrainSize.Y)
	local cellX = math.floor(posX / 4) * 4
	local cellZ = math.floor(posZ / 4) * 4
	local cellY = math.floor(posY / 4) * 4
	local spawnPosition = terrain.Position + Vector3.new(0,terrainSize.Y/2,0) + Vector3.new(posX, posY, posZ) -- adding the half of the terrain size so the npc spawns on the top and not on the core.

	local npcClone = npcModel:Clone()
	npcClone.Parent = workspace
	npcClone:PivotTo(CFrame.new(spawnPosition))
	print("the pane is at", terrain.Position, "and the npc spawned at", spawnPosition)
end

while true do
	wait(1)
	cloneNPC()
end

what does the output say

here are some

  04:31:16.934  the pane is at 0, 0, 0 and the npc spawned at 639, 350, -62  -  Server - Script:17
  04:31:17.952  the pane is at 0, 0, 0 and the npc spawned at -857, 180, -36  -  Server - Script:17
  04:31:18.967  the pane is at 0, 0, 0 and the npc spawned at 406, 177, 875  -  Server - Script:17
  04:31:19.984  the pane is at 0, 0, 0 and the npc spawned at 126, 356, 740  -  Server - Script:17
  04:31:21.000  the pane is at 0, 0, 0 and the npc spawned at -775, 374, -674  -  Server - Script:17
  04:31:22.019  the pane is at 0, 0, 0 and the npc spawned at -95, 130, -429  -  Server - Script:17
  04:31:23.035  the pane is at 0, 0, 0 and the npc spawned at 105, 139, 464  -  Server - Script:17
  04:31:24.067  the pane is at 0, 0, 0 and the npc spawned at 210, 374, 737  -  Server - Script:17
  04:31:25.085  the pane is at 0, 0, 0 and the npc spawned at 362, 214, -14  -  Server - Script:17
  04:31:26.101  the pane is at 0, 0, 0 and the npc spawned at 208, 376, 683  -  Server - Script:17
  04:31:27.117  the pane is at 0, 0, 0 and the npc spawned at 946, 164, -457  -  Server - Script:17
  04:31:28.134  the pane is at 0, 0, 0 and the npc spawned at -78, 129, -358  -  Server - Script:17
  04:31:29.151  the pane is at 0, 0, 0 and the npc spawned at -856, 344, 43  -  Server - Script:17

try printing terrainSize.


ok
2044, 252, 2044
thats what printed out

doesnt look wrong to me, can you add me to teamcreate?

sure thing, one second, just a minute

do i have to be friends with you to do this?

yes. dont worry i’ll unfriend you later after it’s done.

one second, hang on, give me a minute