I am making a One Piece RPG game and I want my devil fruit i'm creating to spawn in designated areas randomly, so it wont be the same fruit at the same spot. Any Ideas?
First we will want to create a table that holds all the positions that a 'devil fruit' could show up.
local positionsTable = {Vector3.new(x,y,z), Vector3.new(x,y,z), Vector3.new(x,y,z)}
If you are unfamiliar with tables, please read the Wiki page linked below:
Each Vector3.new(x,y,z)
is a new position. In the above example there are currently three positions within the table.
Next, we will want to create the function that will choose a position out of the table at random, clone the devil fruit, and place the clone at the random position chosen.
To get a random number, we will use math.random()
. But because math.random()
does not truly give you a 'random' number, we will have to set math.randomseed()
to a number that changes every time the function is run. This can be accomplished by setting math.randomseed()
to tick()
. For more information aboutmath.random()
, click on the link below.
local positionsTable = {Vector3.new(x,y,z), Vector3.new(x,y,z), Vector3.new(x,y,z)} local MyOriginalDevilFruit = game.WhereverYourObjectIs function SpawnDevilFruit() math.randomseed(tick()) local position = positionsTable[math.random(1, #positionsTable)] local clonedFruit = MyOriginalDevilFruit:Clone() clonedFruit.Position = position clonedFruit.Parent = game.Workspace -- Or wherever you want it parented to -- end SpawnDevilFruit() -- fires the function
I hope this helps and sets you on the right track of thinking!
--EDIT--
If this answer helped you or solved your issue, don't forget to upvote and confirm
Use math.random
to first choose a random item (as Programical and Earthkingiv suggest) and then choose a random x/z coordinate. If you just choose any x/z coordinate and you are placing multiple items, the items may overlap. If that's not desirable, you'll want to store locations you've already placed items at and make sure you don't use an x/z coordinate that is too close to any of those locations.
Don't forget to call math.randomseed
once at the beginning of the script (and only do so in one script in your entire place).