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
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.
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.
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
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 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
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.