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.
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.
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.
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.
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)
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.
Hmm, I was thinking about doing that but where would the script be placed? Inside the part script?
Have a ‘generation model’. You could also add folders to store your generated parts and a ‘centre’ part. Put your script in said model.
It is for one part only though.
Then you’re gonna have to do something with PrimaryPart and CFrame. You can read it up here. or here.
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?
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)
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:
local keyPartSpawnerTable = {KeySpawner1,KeySpawner2,KeySpawner3}
local randomIndex = math.random(1,#keyPartSpawnerTable)
local keySpawnPosition = keyPartSpawnerTable[randomIndex].Position
So is KeySpawner1 just a parameter?
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.