Parts spawning within rotated objects issue

I’m trying to spawn objects within a section of part

local digSpawn = workspace.Spawn

for i = 1, 100 do
	local RandomX = (digSpawn.Position.X - (digSpawn.Size.X / 2)) + math.random(0, digSpawn.Size.X)
	local RandomZ = (digSpawn.Position.Z - (digSpawn.Size.Z / 2)) + math.random(0, digSpawn.Size.Z)
	local RandomSpawn = CFrame.new(RandomX, digSpawn.CFrame.Y, RandomZ).Position

	
	local NewPart = workspace.Part:Clone()
	NewPart.Position = RandomSpawn

	NewPart.Parent = workspace
end

and it works fine on regular parts


However if I rotate the same part, parts will have issues spawning

Hi i’m not sure if is this you want but you can try this

local digSpawn = workspace.Spawn

for i = 1, 100 do
	
	local RandomX = math.random(-digSpawn.Size.X/2,digSpawn.Size.X/2)
	local RandomZ = math.random(-digSpawn.Size.Z/2,digSpawn.Size.Z/2)
	local RandomSpawn = digSpawn.CFrame * CFrame.new(RandomX,0,RandomZ)


	local NewPart = workspace.Part:Clone()
	NewPart.CFrame = RandomSpawn

	NewPart.Parent = workspace
end

here my result

1 Like
local rng = Random.new()
function randomPointInPart(part: BasePart): Vector3
	local cf, size = part.CFrame, part.Size
	local offset = size * Vector3.new(
		rng:NextNumber(-0.5, 0.5), 
		rng:NextNumber(-0.5, 0.5), 
		rng:NextNumber(-0.5, 0.5)
	)
	return cf * offset
end

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