How Make Part Spawn Random Around the map

How can I spawn in a defined area, for example a path, Items or Part?

Probably some could suggest to define some positions first and then randomizing them
however I would like to define an area where they can spawn and then a script automatically spawns them each time in a different position within the defined area.

I hope I was clear, if you have any questions feel free to ask!

Thank you.

3 Likes

using math.random. and then generate a random position

examples:

local randomXPosition = math.random(1, 100) -- It will choose a random number between 1 and 100
local randomYPosition = math.random(1, 100)
local randomZPosition = math.random(1, 100)
local finalPosition = Vector3.new(randomXPosition, randomYPosition, randomZPosition)

local part = Instance.new("Part") -- Create a new part
part.Position = finalPosition -- Set the part's position
1 Like

Hello, thank you for you’re reply,

But As I wrote there is a way to take the area of a block and then make spawn another block inside the aerea of that block in a random position?

You can probably do something like this

local area = --Your area part
local areaSize = area.Size * 0.5

local min = area.Position - areaSize
local max = area.Position + areaSize

local part = --The thing to place randomly
--Some other code relating to the thing if needed

part.CFrame = CFrame.new(
	math.random(min.X, max.X), 
	min.Y + part.Size.Y * 0.5,
	math.random(min.Z, max.Z)

part.Parent = workspace --Or where you want to parent it

Though this will place the parts randomly but the Y will always be the bottom of the block, mostly if you want to put things on the ground, if that is not what you want, then change the 2nd argument in the CFrame.new to be the same as the other 2, except you reference the Y property,

7 Likes

Thank you Very Much! is Working.

1 Like

where do I need to put this script

Just put it in workspace, its a server script.

1 Like

thanks but it doesn’t work sadly

Does it error? I’ll test it out

robloxapp-20230317-2240038.wmv (1.1 MB)
I’m trying to make the books spawn in random locations in a certain area

Also sorry for the video lag lol

Works completely fine for me… here’s the script:

local area = workspace.Part
	
local areaSize = area.Size * 0.5

local min = area.Position - areaSize
local max = area.Position + areaSize


local part =  Instance.new("Part") --The thing to place randomly
	--Some other code relating to the thing if needed
part.CFrame = CFrame.new(
	math.random(min.X, max.X), 
	min.Y + part.Size.Y * 0.5,
	math.random(min.Z, max.Z)
)
part.Parent = workspace --Or where you want to parent it

This script worked fine, just change the area variable to the area path that you want, and the part variable to whatever you want…

1 Like

Don’t know why it doesn’t work for me

Can you show me your script? Is it a server script in workspace?

Also i’m using an union,is it fine

Yea, its completely fine. Any type of part works with the script.

It’s a script in the workspace