I need help with math.random

so i want this minigame where it randomly selects tiles and deletes them until there are no tiles left, but i am total garbage with math.random and have no idea to implement it. any help??

local tiles = script:GetDescendants()
local rantiles = tiles[math.random(3, #tiles)]
local gamestatus = game.ReplicatedStorage.GameStatus

countdown = 5
for i = countdown, 0, -1 do
	gamestatus.Value = i
	wait(1)
end

for i = #tiles, 0 do
	rantiles:Destroy()
    wait(1)
end
for i=1,#tiles do
   tiles[math.random(1,#tiles)]:Destroy()
   wait(1)
end
1 Like

This will return only 1 tile. If you want to destroy random tiles until there are none left, try this.

local Tiles = script:GetDescendants()

while (#Tiles > 0) do
    local Index = math.random(1, #Tiles)

    Tiles[Index]:Destroy()
    table.remove(Tiles, Index)
    wait(1)
end

This has the potential to not destroy all of the tiles because math.random(1, #tiles) can return the same index. You’re never removing any of the tiles that are destroyed from the tiles array so it has the chance to reference an already deleted tile and then it will try to delete it agian.

for i=1,#tiles do
   tiles[math.random(1,#script:GetDescendants())]:Destroy()
   wait(1)
end

That just causes another issue.

From glance, you will have useless iterations that do nothing, or you will have an Attempt to index nil with 'Destroy', or bad argument #1 to 'random' (interval is empty)

Just use a while loop. Like I stated in my first reply.

1 Like
for i=1,#tiles do
   local a = tiles[math.random(1,#script:GetDescendants())]
   if a~=nil then
      a:Destroy()
   end
   wait(1)
end

oh yeah i also have an issue now. its not an issue with your code, rather that i forgot to explain that the tiles i have are models which contain two parts. so when the script runs, it removes 1 part at a time instead of the entire model. i forgot how to do this

Which leads to parts that will still be left over. Here I even RAN the code to show you the issue.

Literally just do what I suggested and stop making these stupid hot fixes that cause more issues

Are you trying to remove 1 random part from a random model? Or are you trying to randomly remove all of the parts from a specific model, then go to the next and do the same thing.

i’m trying to remove all the parts from a model names tile

Can you show a picture of explorer and tell me which parts you’re trying to destroy

and there are 400 total tiles that its choosing from

image
i put them into the script to avoid any issues with getchildren() and folders

if v.Name=="Tile" then

yeah but it still needs to go through and check the folders if they are located in the same spot of hierarchy, therefore breaking the script

I think something like this? I’m not entirely sure because I’m still confused on what you’re trying to do.

Iterate over all of the children then iterate over their children.

local TileModels = script:GetDescendants()
local Tiles = {}

for i, Tile in pairs(Tiles) do
    local Parts = Tile:GetChildren()

    for j, Part in pairs(Parts) do
        table.insert(Tiles, Part)
    end
end

while (#Tiles > 0) do
    local Index = math.random(1, #Tiles)

    Tiles[Index]:Destroy()
    table.remove(Tiles, Index)
    wait(1)
end

i tried the script and its not destroying the tiles anymore for some reason

im just trying to destroy tiles one model at a time which are located inside the script

Oh, okay. Try something like this.

local Tiles = script:GetChildren()

for i, Tile in pairs(Tiles) do
    local Parts = Tile:GetChildren()

    while (#Parts > 0) do
        local Index = math.random(1, #Parts)
    
        Parts[Index]:Destroy()
        table.remove(Parts, Index)
        wait(1)
    end
end

It will iterate through each model, then randomly remove parts in that model until it has no more parts, then it will move onto the next tile model.