Random Part Spawning Error

I am trying to have parts spawn on the floor of this plain (shown below) , to act as mines in a minefield. Yet the part seem to be spawning anywhere they want to be not in the restricted area.

My current script spawns the part and adds it in to the folder as intended, yet not in the correct position on the map.

Here is my current script along with the properties of my desired spawn area. I can’t seem to figure this out.

local MineFolder = Instance.new("Folder")
MineFolder.Name = "MineFolder"
MineFolder.Parent = game.Workspace

while wait (2) do
	local pos1 = math.random(-185, 10)
	local pos2 = math.random(-185, 10)

	
	local Mine = Instance.new("Part", MineFolder)
	Mine.Size = Vector3.new(2,0.1,2)
	Mine.Shape = Enum.PartType.Ball
	Mine.BrickColor = BrickColor.Random()
	Mine.Material = Enum.Material.Neon
	Mine.Transparency = 0
	Mine.Anchored = true
	Mine.Position = Vector3.new(pos1, 30, pos2)
end

Properties of MineFieldPart
Size : 28, 1 ,144.25
Position : 217 ,0.5 ,25.375
Orientation : 0, 0, 0
Origin Position : 217 ,0.5 ,25.375
Origin Orientation 0, 0 ,0

Thank you to anyone who helps on this!! I really think I have tried it all and cant get it to work.

Do you have a reference to the minefield part? If so you could offset the random positions to the position of the part. (side note, the mines might be super small due to the size scaling you have put for them.)

both pos1 and pos2 are using the same number range, although your restricted area is not a square. could this be the problem?

I tried to change the positions but now I get an invalid argument.

ServerScriptService.Script:6: invalid argument #2 to ‘random’ (interval is empty) - Server - Script:6

local MineFolder = Instance.new("Folder")
MineFolder.Name = "MineFolder"
MineFolder.Parent = game.Workspace

while wait (2) do
	local pos1 = math.random(230.875, -46.625)
	local pos2 = math.random(203.125, 97.375)

	
	local Mine = Instance.new("Part", MineFolder)
	Mine.Size = Vector3.new(2,0.1,2)
	Mine.Shape = Enum.PartType.Ball
	Mine.BrickColor = BrickColor.Random()
	Mine.Material = Enum.Material.Neon
	Mine.Transparency = 0 -- set to 1 for mine
	Mine.Anchored = true
	Mine.Position = Vector3.new(pos1, 30, pos2)
end

when using math.random you have to put the smaller integer before the larger one

local pos1 = math.random(-46.625,  230.875)
local pos2 = math.random(97.375, 203.125)

The actual mine part is (2,0.1,2)

The MineFieldPart is (217, 0.5, 25.375)

But if thats the position of the MineFieldPart that im putting in how will I make it so its only in the selected area i want?

this should work

edit: this only works if the parts orientation is 0 or 180

local part = (the part you want it to be on)

local function spawnMine()
local placeholder = Instance.new(“Part”)
placeholder.Parent = workspace
local random1 = math.random(-part.Size.X,part.Size.X)/2
local random2 = math.random(-part.Size.Z,part.Size.Z)/2
placeholder.Position = part.Position+Vector3.new(random1,part.Size.Y/2,random2)
end

spawnMine()

if the x value of the Vector3 is on one corner and the z value is on the opposite corner, then it should choose a position within the part.

This worked! Thank you so much!!!

Thank you @Bonusizawesome aswell for helping!

1 Like

glad I could help, if at all.

good luck on your game!