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