Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I spawn a random item at a certain spot?

Asked by
jm34 7
7 years ago

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?

0
Make a table and have it iterate through the table cloning the fruit and placing it somewhere Earthkingiv 51 — 7y
0
Picking random element from a table, and maybe GetChildren... Google it. Programical 653 — 7y

2 answers

Log in to vote
2
Answered by 7 years ago
Edited 4 years ago

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:

Tables

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.

math.random()

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--

Added in "clonedFruit.Parent" line

If this answer helped you or solved your issue, don't forget to upvote and confirm

0
Ok I'll give it a try! jm34 7 — 7y
0
So I plugged it all into a script and everything was working and I gave it three spots to spawn but it just didn't spawn anything, nothing in the output said anything went wrong. jm34 7 — 7y
1
You forgot to give it a parent! zuup123 99 — 4y
0
Thanks, zuup123! SteamDemand 312 — 4y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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).

http://wiki.roblox.com/index.php?title=Random_numbers

Answer this question