How could I make a part spawn randomly in a given location?

I want to create a script that makes my part spawn randomly in a given location.

My part is a key btw and is in replicated storage. I don’t need entire scripts, just a base that is all. Thank you.

4 Likes

This isn’t enough information, how do you want it to work? completely random, probably using math to do it or at a specific time, on touch on click ect. I also want to know what you are going to use it for so I can aim for it.

1 Like

I want it to be completely random in a specific zone. I was think about using math.random and Region3’s but I don’t know how to execute something like this.

1 Like

Generate with math.random, X Y Z coordinates within parameters.

local radius = 100 - - maximum radius from the point of generation

local x = math.random(0,radius) 
local y = math.random(0,radius) 
local z = math.random(0,radius) 

part.Position = Vector3.new(x,y,z)
9 Likes

If you are going for the math.random route, make a script inside server script service and then make a function that clones your part and parents it to the workspace. You will then have to make the x, y, and z. Make variables for these by doing math.random(number, number). Then set the position by saying part.Position = Vector3.new(variablename, variablename, variablename)

Edit: Oh someone said it before me.

1 Like

Hmm, I was thinking about doing that but where would the script be placed? Inside the part script?

2 Likes

Have a ‘generation model’. You could also add folders to store your generated parts and a ‘centre’ part. Put your script in said model.

1 Like

It is for one part only though.

1 Like

Then you’re gonna have to do something with PrimaryPart and CFrame. You can read it up here. or here.

1 Like

So I did what you said and place the script in the part. But it doesn’t actually place it in the location I want. Would I have to use Region3?

1 Like

I’m not too experienced with Region3 so I can’t say much. I’d recommend spawning it at the centre part and use Vector3 to position it.

part.Position = centrePart.Position + Vector3.new(x, y, z)
2 Likes

Yeah if you want to spawn in a location you want but randomly, then you have to all the possible locations which you want the key to spawn in.

Yeah to define the places you want the key to spawn in the easiest way is to follow @JasonSpero suggestion of putting an invisible anchored non-collidable part that will act as the spawn point for the Key.

To choose which part is randomly, here are the possible steps:

  1. Store the key spawner parts in a table
local keyPartSpawnerTable = {KeySpawner1,KeySpawner2,KeySpawner3}
  1. Choose a random part from the table using math.random or any other way
local randomIndex = math.random(1,#keyPartSpawnerTable)
  1. Then access the parts located in the table
local keySpawnPosition = keyPartSpawnerTable[randomIndex].Position
1 Like

So is KeySpawner1 just a parameter?

1 Like

Uhh it’s a invisible anchored non-collidable part that will act as the spawn point for the Key.
So its a BasePart which you place in the map where you want the key to spawn.

1 Like