How do I spawn things in a random position but only on a baseplate

Ok, so im trying to make banana peels spawn on the baseplate of my map. I want to make them spawn in random positions but they have to be on the baseplate. If anyone knows how to do this please let me know since im adding this in my next update. Thanks!

3 Likes

You can do that by simply cloning the banana peel from ReplicatedStorage and parent it to the baseplate, and then set the .Position by using 2 (X, Y) or 3 (X, Y, Z) math.random() numbers.

Example;

local randomX = math.random(-1024, 1024) --//Generates a random number between the two given numbers
local randomZ = math.random(-1024, 1024) --//2048 is the size of the baseplate

local clonedBanana = game.ReplicatedStorage.BananaPeel --//Path to the part to clone
clonedBanana.position = Vector3.new(randomX, 1, randomZ)
4 Likes

Is there a way to calculate these numbers with a part? Like a function if I wanted to do that for game.Workspace.Part

2 Likes

Please elaborate further.

Do you mean calculate the boundaries of the randomX- and Z positions depending on the size and position in the game?

1 Like

Yes! Thats what I meant, is there any specific way to calculate the boundaries?

1 Like

Method of Creating Offsets Based on size:
(ObjectPosition + ObjectSize/2)

You start at the Objects current position, which marks the exact center of the object, basically meaning that the part is split up into four sections, with two of them per axis. One section marks the positibe side, while on the other side will be the negative variant.
(Keep in mind, both sides are the same number, and always will be)

Using the Parts Size Property, we can calulate an area using this method by obtaining the X Axis, and the X Axis, with the Y Axis being entirely optional in case you need to make offsets to how high it will be.

local Size = Baseplate.Size

-- our two components to calulate
local x = Size.X/2
local z = Size.Z/2

local offset = Vector3.new(math.random(-x, x) 0, math.random(-z, z))
-- -x and -z correspond to the left and back of the baseplate
-- x and z correspond to the right, and front of the baseplate
-- think of it like a coordinate plane with the Baseplates center being the origin
Object.Position = Baseplate.Position + offset

If you are using a different shape (like a sphere or circle), it will require a different form of calulation, which i dont know how to do at this time.

2 Likes

Ohh! It makes sense now, thank you so much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.