How to make part spawn at a random spot above a part

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to spawn a part (Part A) on top of Part B at a random x and y position of Part B.

  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to achieve it.

Relate picture:
image

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I am looking for answer/solution in the lovely Devforum site!

Thank you <3 !

2 Likes

So, this is actually sort of simple to do.

local Random = Random.new() -- we'll use this for randomizing, later.
local spawnedPart = PartA -- the item that will spawn.
local spawnZone = PartB -- the part where other things will spawn in.

-- what we'd have to do in order for it to spawn at random places of
-- PartB, is by getting it's size and randomizing the x and z in PartA's
-- position.
-- (you said at random x and y position, but y expresses the width.)
-- so in order to do that...

local zoneSize = spawnZone.Size
local spPosition = spawnZone.CFrame:VectorToWorldSpace(
     Vector3.new(
          spawnZone.CFrame.RightVector * Random:NextNumber(-zoneSize.X / 2, zoneSize.X / 2), 
          zoneSize.Y / 2, 
          spawnZone.CFrame.RightVector * Random:NextNumber(-zoneSize.Z / 2, zoneSize.Z / 2)
))
-- we have to divide the size by 2, for one half of the part.
-- we also had to convert the position into WorldSpace, because it's expressed
-- from the parts space.

spawnedPart.Position = spPosition

This should work… if it doesn’t let me know!

1 Like

Sorry for mistake! I didn’t notice that. It doesn’t work-

1 Like

Do you mind sharing the results? I’m testing the script to see what’s the problem right now.

1 Like

The green part spawn at the middle of the spawnLocation
image

1 Like

You could try doing this method.

local Part1 = game.workspace.Part1
local Part2 = game.workspace.Part1

local RandomX = math.random(15,30)
local RandomZ = math.random(15,30)

Part1.Position = Part2.Position - Vector3.new(RandomX, 0 , RandomZ)

See if this works.
1 Like

I honestly dont know if this would work but worth a try

local PartB = PartB 
local PartA = PartA

local x, y, z = (PartB.Size.X/2), (PartB.Size.Y/2), (PartB.Size.Z/2)
local randomx, randomy, randomz = (math.random(-x, x)), (math.random(-y, y)), (math.random(-z, z))

PartA.Position = PartB.Position + Vector3.new(randomx, randomy, randomz)
1 Like

The reason it didn’t work was because I used VectorToWorldSpace instead of PointToWorldSpace (VectorToWorldSpace rotates it, PointToWorldSpace translates it (moves it)). And then, using LookVector and RightVector made the values consistent, which is weird…

So the new script would be this:

task.wait(5)
local Randomnew = Random.new() -- we'll use this for randomizing, later.

-- if you want it to be in integer studs, change it to:

-- local Randomnew = math.random

local spawnedPart = workspace.PartA -- the item that will spawn.

local spawnZone = workspace.PartB -- the part where other things will spawn in.

-- what we'd have to do in order for it to spawn at random places of

-- PartB, is by getting it's size and randomizing the x and z in PartA's

-- position.

-- (you said at random x and y position, but y expresses the width.)

-- so in order to do that...

local zoneSize = spawnZone.Size

local spPosition = spawnZone.CFrame:PointToWorldSpace(

Vector3.new(

Randomnew:NextNumber((zoneSize.X / 2) / -1, zoneSize.X / 2),

spawnZone.CFrame.UpVector * zoneSize.Y / 2,

Randomnew:NextNumber((zoneSize.Z / 2) / -1, zoneSize.Z / 2)

)

)

-- we have to divide the size by 2, for one half of the part.

-- we also had to convert the position into WorldSpace, because it's expressed

-- from the parts space.

print(spPosition)

spawnedPart.Position = spPosition

This would work, if you didn’t want to be specific in what place it’ll be.
This takes Part1 way off the position where it should be, which is why I used the size of PartB (your Part2) to get the position from it’s size.

local PartA = game.workspace.PartA
local PartB = game.workspace.PartB

local Height = 1 --Height of part A above part B.
local RandomXPosition = math.random(-1 * PartB.Size.X / 2, PartB.Size.X / 2) --Random x-pos from size of part B.
local RandomZPosition = math.random(-1 * PartB.Size.Z / 2, PartB.Size.Z / 2) --Random z-pos from size of part B.

PartA.Position = PartB.Position + Vector3.new(RandomXPosition, Height, RandomZPosition) --Setting position.
2 Likes

Thank you guys for helping me to achieve this.