Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

2 comments

0

Make a table and have it iterate through the table cloning the fruit and placing it somewhere

0

Picking random element from a table, and maybe GetChildren... Google it.

2 answers

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 about math.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

4 comments

0

Ok I'll give it a try!

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.

1

You forgot to give it a parent!

0

Thanks, zuup123!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.