Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to randomize tile placement?

I have five different tiles called, "TileOne", "TileTwo" etc all the way up to five.

I have basically copied and pasted these all over the place, but in some areas you can clearly see repeating and I want them to be as randomized as possible.

I need help coming up with a script that I can just run in the output that randomizes the tiles.

I would like for every existing tile (one, two, three, four, five) to be completely randomly replaced with the same tile set.

2 comments

0

Are the tiles all being placed on even ground?

0

They are all on perfectly even ground at exactly 0.5 on the y axis

2 answers

✓ Accepted answer

This will take your existing tiles and replace them with a randomly selected replacement tile.

local tileTemplates = {
    game.ServerStorage.Tiles.TileOne,
    game.ServerStorage.Tiles.TileTwo,
    game.ServerStorage.Tiles.TileThree,
    game.ServerStorage.Tiles.TileFour,
    game.ServerStorage.Tiles.TileFive
}

local rand = Random.new() -- Use the new Random class for better results
local existingTiles = game.Workspace.TileFolder:GetChildren()
for _, tile in pairs(existingTiles) do
    local replacementTile =
        tileTemplates[rand:NextInteger(1, #tileTemplates)]:Clone()
    replacementTile.Parent = game.Workspace.TileFolder
    replacementTile.CFrame = tile.CFrame
    tile:Destroy()
end

(Hint: You can even copy-paste this into the command bar)

1 comment

1

This is EXACTLY what I need thank you very much.

You should use the math.random() function.

The following is an example of how you could do this. The program randomly generates x and z positions for the tiles within a specified boundary.

Just make sure one tile is inside ServerStorage, and is named 'Tile'

NumberOfTiles = 20 -- no. of tiles in total

xsize = 60 --x by z size of the area the tiles can spawn in
zsize = 60

xpos = 0-- x and z position coords of where the area the tiles will spawn in will be
zpos = 0

math.randomseed(tick())


for i = 1,NumberOfTiles do
	xrandom = math.random(1, xsize)
	zrandom = math.random(1,zsize)
	print(xrandom,zrandom)
	tile = game.ServerStorage.Tile:Clone()
	tile.CFrame = CFrame.new(xrandom+xpos,0.5,zrandom+zpos) -- 0.5 is Y coord height
	tile.Parent = workspace
	
end

NOTE: This program will not stop tiles spawning inside of each other.

1 comment

0

Thank you, but what I need is for the existing tiles to be swapped out for a random one. The position should not change at all. It's also using the same tile set I just need the tiles randomized.

This discussion is preserved as it was. New answers, comments, and votes are closed.