Randomize position based on a parts position, and rotation..?

Hey! I’m having a very hard time on how to randomize a parts position in another part, with the old parts rotation too…?

Here’s an example:

GREEN - I can do a position in a part without rotation.
RED - No clue, part just acts like the green part, without any rotation. So it’ll still place in corners that aren’t there.
BLUE - no. clue. at. all.

Here’s what I have:

local x = randomSpawnInPoint.Size.X / 2
local z = randomSpawnInPoint.Size.Z / 2
local endPosition = randomSpawnInPoint.Position + Vector3.new(math.random(-x, x), randomSpawnInPoint.Position.Y, math.random(-z, z))

Help would be awesome, thank you!

1 Like

Try doing this:

local x = randomSpawnInPoint.Size.X / 2
local z = randomSpawnInPoint.Size.Z / 2
local endPosition = randomSpawnInPoint.Position + randomSpawnInPoint.CFrame*Vector3.new(math.random(-x, x), randomSpawnInPoint.Position.Y, math.random(-z, z))

This should change the XYZ axis to the parts axis instead the global XYZ axis.

3 Likes

Okay, I tested it out, and it looked like it was spawning right, but they all spawned in a far away spot. I probably should’ve mentioned this first, but i’m trying to move a model! Here’s the code for setting it’s position:

cloned:SetPrimaryPartCFrame(CFrame.new(endPosition,Vector3.new(0,0,0)))

(cloned is the model)

1 Like