How to Get Random Position in Region3

I’m trying to make parts randomly spawn within a Region, here’s what I’ve tried:

local x = humanoidRoot.Position - Vector3.new(20, 3, 20)
local y = humanoidRoot.Position + Vector3.new(20, 10, 20) 
local region = Region3.new(x, y)

local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false

local x, z = region.Size.X, region.Size.Z
part.Position = Vector3.new(math.random(x, z), 1, math.random(x, z)) 

--[[ they only spawn in one place, since X and Z are both 40
what should i do to get a random position?
--]]

part.Parent = workspace

Bassically I dont know what should go in the parenthesis for math.random()

I think you should use math.random correct me if im wrong.

yes i agree, that is what i have done

Vector3.new(math.random(-x/2, x/2), 1, math.random(-z/2, z/2))

1 Like

Just what I needed thanks lol im dumb

Vector3.new(math.random((-x/2) + part.Size.X/2, (x/2) - part.Size.X/2), 1, math.random((-z/2) + part.Size.Z/2, (z/2) - part.Size.Z/2))

you can do something like this to keep the part from spilling over the region

2 Likes