Help With Spawning Parts at Certain Locations

Here’s the general outline of the script:

In the workspace, there is a board with tiles. What I’m trying to do is script a button that spawns a Part at the tiles upon clicking it, but the part is assigned randomly to the tiles each time.

Basically, when the button is clicked, I want the part to spawn at any of the tiles on the board. (16 tiles on the board)

I started with using math.random to random pick one of the 16 tiles, and then assign each tile one of the numbers given in math.random. This method takes way too long, is there another shorter way?

Make a Table that has every title in it, loop through the table, and then you clone the part and add its position.

Example

local Tiles = workspace.Folder:GetChildren()

function createPart()
     local newPart = Instance.new("Part")
     newPart.Position = Tiles[math.random(#Tiles.Position)]
end)
1 Like

Tried your script:

local Tiles = {}

for i, v in pairs(workspace.Folder:GetChildren()) do
	table.insert(Tiles, v)
end

function createPart()
	local newPart = Instance.new("Part", workspace)
	newPart.Position = Tiles[math.random(#Tiles)].Position
end


createPart()
createPart()
1 Like