Spawning items inside a circle

Hello, I managed to make parts spawn in a circle but how would I go about spawning parts inside the circle

This is my script

	local function NewStick(i)		
		local Stick = ItchRemote.QuestObject.DailyQuestStick:Clone()
		Stick.Name = "Stick"..i
		for _,part in pairs(Stick:GetChildren()) do
			if part:IsA('BasePart') then
				part.Anchored = true
				part.CanCollide = false
			end
		end
		
		local PickUp = Instance.new('ProximityPrompt',Stick.PrimaryPart)
		PickUp.ObjectText = "Herbs"
		PickUp.ActionText = "Pick Up"
		PickUp.HoldDuration = .75
		PickUp.KeyboardKeyCode = Enum.KeyCode.E
		PickUp.RequiresLineOfSight = false
		
		Stick.Parent = workspace
		--- HEREEEEEEEEEEEE
		local radius = 150
		local angle = (math.pi*2/math.random(1,20)) * i  
		local x = radius * math.cos(angle) 
		local y = radius * math.sin(angle)

		local part = Stick
		part:SetPrimaryPartCFrame(HMRP.CFrame * CFrame.new(x, 50, y))  
		part:SetPrimaryPartCFrame(CFrame.new(part.PrimaryPart.CFrame.p, HMRP.CFrame.p))
		
		local RayParam = RaycastParams.new()
		RayParam.FilterDescendantsInstances = {game.Workspace.Baseplate}
		RayParam.FilterType = Enum.RaycastFilterType.Whitelist
		
		local ray = workspace:Raycast(part.PrimaryPart.Position,Vector3.new(0,-60,0),RayParam)
		
		if ray then
			Stick:MoveTo(Vector3.new(Stick.PrimaryPart.Position.X,ray.Position.Y,Stick.PrimaryPart.Position.Z) )
			Stick:SetPrimaryPartCFrame(CFrame.new(Stick.PrimaryPart.Position.X, Stick.PrimaryPart.Size.Y / 2, Stick.PrimaryPart.Position.Z))
		end
		
		GPS(Stick.PrimaryPart)
		
		PickUp.Triggered:Connect(function()
			Increment(1)
			
			Stick:Destroy()
		end)
		
	end
	
	for i = 1,amt do
		NewStick(i)
	end
end
2 Likes

First, get a random X or Z value that is smaller than the radius or larger than -radius. This can be used to generate a chord as it gives the distance from the centre. We can use a perpendicular bisector to find the length of the chord


The chord gives us the range of the random X or Z value allowing it to be in a circle.

I substituted 10 as the random value. Pythagoras can be used to find the length of the chord.


This gives the range of the random of the other axis value.

local radius = ??
local X = math.random(-radius,radius)
local range = math.sqrt((radius^2)-(X^2))
local Z = math.random(-range/2,range/2)
local Y = ???
local position = Vector3.new(X,Y,Z)
4 Likes