How to make parts spawn within a zone

I’m trying to make parts spawn within a zoneplus zone, but their Y position never changes no matter how small I make the part the zone is based off. Here is my code:

local function generateRandomPosition(region: Region3)
	local x, y, z = region.Size.X, region.Size.Y, region.Size.Z
	local position = region.CFrame.Position
	local randomPosition = Vector3.new(
		position.X + math.random(-x / 2, x / 2), 
		position.Y,
		position.Z + math.random(-z / 2,  z / 2)
	)
	
	return randomPosition
end
1 Like

if it’s a zoneplus zone then you can just use :getRandomPosition or something like that. Otherwise just use this

local function generateRandomPosition(region: Region3)
	local x, y, z = region.Size.X, region.Size.Y, region.Size.Z
	local position = region.CFrame.Position
	local randomPosition = Vector3.new(
		position.X + math.random(-x / 2, x / 2), 
		position.Y + math.random(-y / 2, y / 2),
		position.Z + math.random(-z / 2,  z / 2)
	)
	
	return randomPosition
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.