Help with increasing part position?

Hello!

I’m currently still working on my project, and i figured how to use random positions, but i want to increase that with a sort of radius or something.

As you can see, the red circle is the zone that i want stuff to spawn in(maybe a little bit bigger…), but i’m not entierly sure about changing the position as i’m still learning it…

The area that the stuff spawn in right now is very big but i want it to be bigger…

Here is a piece of code:

	truss.Position = Vector3.new(math.random(-114, 12),6,math.random(-124, 33))

I want to make a radius that it spawns in or some sort of zone…

How can i do this?

I’m still learning coding :slight_smile:

Cheers! :wink:

local trussFolder = workspace.trussFolder --folder with all the objects who you want to spawn

function spawnInCircle(object, center, radius)
	local r = math.random 
	local abs = math.abs
	local pos
	repeat 
		pos = Vector3.new(r(-radius, radius), object.Position.Y, r(-radius, radius)) 
	--pythagorean basically
	until math.sqrt(abs(pos.X)^2+abs(pos.Z)^2) <= radius
	pos += center --aligning to center after the above calculations run
	local c = object:Clone()
	c.Position = pos 
	c.Parent = workspace 
end

for _, truss in pairs(trussFolder:GetChildren()) do 
	local spawnPoint = Vector3.new(0, 0, 0)
	local radius = 120
	spawnInCircle(truss, spawnPoint, radius)
end

Thanks, but do you think this would fit with my main script?

script.Parent.ProximityPrompt.Triggered:Connect(function()

local event = math.random(1,22)

local timer = math.random(30,60)

if event == 1 then

local Police = game.ServerStorage.Police:Clone()

Police.Parent = workspace

script.Parent.Parent.Assets.Police:Play()

wait(timer)

Police:Destroy()

elseif event == 2 then

local Marine = game.ServerStorage.Marine:Clone()

Marine.Parent = workspace

wait(timer)

Marine:Destroy()

elseif event == 3 then

local Zombie = game.ServerStorage.Zombie:Clone()

Zombie.Parent = workspace

wait(timer)

Zombie:Destroy()

elseif event == 4 then

local Helicopter = game.ServerStorage.Helicopter:Clone()

Helicopter.Parent = workspace

wait(timer)

Helicopter:Destroy()

elseif event == 5 then

local battlebot = game.ServerStorage.FirePillar:Clone()

battlebot.Parent = workspace

battlebot.Position = Vector3.new(math.random(-114, 12),8,math.random(-124, 33))

wait(timer)

battlebot:Destroy()

elseif event == 6 then

local tree = game.ServerStorage.Tree:Clone()

tree.Parent = workspace

tree.Position = Vector3.new(math.random(-114, 12),8.466,math.random(-124, 33))

wait(timer)

tree:Destroy()

elseif event == 7 then

local wall = game.ServerStorage.Wall:Clone()

wall.Parent = workspace

wall.Position = Vector3.new(math.random(-114, 12),15.1,math.random(-124, 33))

wait(timer)

wall:Destroy()

elseif event == 8 then

game.Workspace.Gravity = math.random(1, 500)

elseif event == 9 then

local bunker = game.ServerStorage.BunkerClone:Clone()

bunker.Parent = workspace

bunker.Position = Vector3.new(math.random(-114, 12),8.9,math.random(-124, 33))

script.Parent.Parent.Assets.CloneBunker:Play()

wait(timer)

bunker:Destroy()

elseif event == 10 then

local stone = game.ServerStorage.Stone:Clone()

stone.Parent = workspace

stone.Position = Vector3.new(math.random(-114, 12),7.5,math.random(-124, 33))

wait(timer)

stone:Destroy()

elseif event == 11 then

local bomb = game.ServerStorage.ElectricBomb:Clone()

bomb.Parent = workspace

bomb.Position = Vector3.new(math.random(-114, 12),0.2,math.random(-124, 33))

script.Parent.Parent.Assets.ElectricBombSound:Play()

wait(timer)

bomb:Destroy()

elseif event == 12 then

local mwall = game.ServerStorage.MovingWall:Clone()

mwall.Parent = workspace

mwall.Position = Vector3.new(math.random(-114, 12),15.1,math.random(-124, 33))

wait(timer)

mwall:Destroy()

elseif event == 13 then

local force = game.ServerStorage.ForcefieldPart:Clone()

force.Parent = workspace

force.Position = Vector3.new(math.random(-114, 12),5.4,math.random(-124, 33))

wait(timer)

force:Destroy()

elseif event == 14 then

local sand = game.ServerStorage.FallingSand:Clone()

sand.Parent = workspace

sand.Position = Vector3.new(math.random(-114, 12),105,math.random(-124, 33))

wait(timer)

sand:Destroy()

elseif event == 15 then

local fire = game.ServerStorage.FirePart:Clone()

fire.Parent = workspace

fire.Position = Vector3.new(math.random(-114, 12),0.4,math.random(-124, 33))

wait(timer)

fire:Destroy()

elseif event == 16 then

local branch = game.ServerStorage.BigBranch:Clone()

branch.Parent = workspace

branch.Position = Vector3.new(math.random(-114, 12),130,math.random(-124, 33))

wait(timer)

branch:Destroy()

elseif event == 17 then

local stick = game.ServerStorage.LargeStick:Clone()

stick.Parent = workspace

stick.Position = Vector3.new(math.random(-114, 12),23.8,math.random(-124, 33))

wait(timer)

stick:Destroy()

elseif event == 18 then

local bush = game.ServerStorage.GrassBush:Clone()

bush.Parent = workspace

bush.Position = Vector3.new(math.random(-114, 12),3.993,math.random(-124, 33))

wait(timer)

bush:Destroy()

elseif event == 19 then

local truss = game.ServerStorage.Truss:Clone()

truss.Parent = workspace

truss.Position = Vector3.new(math.random(-114, 12),6,math.random(-124, 33))

wait(timer)

truss:Destroy()

elseif event == 20 then

local cloud = game.ServerStorage.Cloud:Clone()

cloud.Parent = workspace

cloud.Position = Vector3.new(math.random(-114, 12),45.8,math.random(-124, 33))

wait(timer)

cloud:Destroy()

elseif event == 21 then

local cacti = game.ServerStorage.Cactus:Clone()

cacti.Parent = workspace

cacti.Position = Vector3.new(math.random(-114, 12),4.3,math.random(-124, 33))

wait(timer)

cacti:Destroy()

elseif event == 22 then

local plat = game.ServerStorage.Platform:Clone()

plat.Parent = workspace

plat.Position = Vector3.new(math.random(-114, 12),23.9,math.random(-124, 33))

wait(timer)

plat:Destroy()

end

end)

I’m just asking, because im not very sure all that would go into the main script…

Instead of using spawnInCircle which also clones the object, just position it using the function below(place on top of your script):

function PositionInCircle(object, center, radius)
	local r = math.random 
	local abs = math.abs
	local pos
	repeat 
		pos = Vector3.new(r(-radius, radius), object.Position.Y, r(-radius, radius)) 
		--pythagorean basically
	until math.sqrt(abs(pos.X)^2+abs(pos.Z)^2) <= radius
	pos += center --aligning to center after the above calculations run
	object.Position = pos 
end

And replace every object.Position = Vector3.new(x, y, z) with PositionInCircle(object, center, radius).