Getting random part from a folder not working

I am scripting something that requires me to get a random part from a folder and I have looked at posts but to no avail, this is all it should take according to posts. tiles is a variable that is the children of the folder I need to get the part from. Does anyone have this problem and or a solution? Thanks!

local randomTile = tiles[math.random(1,#tiles)]

1 Like

When you declared the value of tiles did you get the children. For example did you do local tiles = Folder:GetChildren()
That could be one reason it isn’t working.

I did this, so yes.

local tiles = TileList:GetChildren()

Well if you put

print(randomTile) 

it should output a random number between 1 and the number of tiles, it will not output a random tile/part, if that makes sense.

One reason I had to come to Dev Forum for this is because my script was the same but then I suddenly got the error. Is there any way to correctly pick a random part then? Because I am at a lost .-.

What error did you get? Also, when using math.random you can pass a single argument if the minimum value is 1.

For instance:

math.random(10) -- number between 1 and 10

When I do that, it says one of the arguments is not filled in/nil basically, or it says that it is a number but it is supposed to be a part.

Just to give you more info, this is the entire script that needs the random tile.

local TileList = game.Workspace.TileList
local tiles = TileList:GetChildren()
local Timer = game.StarterGui.MainUI.Mainframe.Timer

local function FireLasers()
	
		for i = 1,10,1 do
			local randomTile = tiles[math.random(1,#tiles)]
			print("Tile "..randomTile.Name.." was choosen!")
	
			wait(0.5)
		for i = 1,10,1 do
			wait(0.01)
			randomTile.Transparency = randomTile.Transparency + 0.1
		end
	
			randomTile.CanCollide = false
			wait(0.5)
	
		for i = 1,10,1 do
			wait(0.01)
			randomTile.Transparency = randomTile.Transparency - 0.1
		end
	
			randomTile.CanCollide = true
			wait(0.01)
	
	end
end

Ok so you could do something like:

local count = 0
	    for i,v in pairs(tiles)do
            if count == randomTile then
--Do what you need to do to the random part random part will be v so if you want to destroy it do v:destroy()
            end
	        count = count + 1
		end

If it doesn’t work you might need to change it to

local count = 0
	    for i,v in pairs(tiles)do
            if count == toNumber(randomTile) then
--Do what you need to do to the random part random part will be v so if you want to destroy it do v:destroy()
            end
	        count = count + 1
		end

I didn’t test this but I think it will work. Let me know if it works or not.

Are you 100% certain there is at least 1 child of your TileList?
If #tiles is 0, math.random(1, 0) will return an error.

TileList in the workspace has 9 children, each a part with numerical games for their positions.

Could you paste the exact error you’re getting? It may be more clear once we understand what you’re receiving.

It looks like you’re missing an end to close off one of your for-loops.

EDIT: never mind just weird indentation

I have not gotten an error pertaining to that so I don’t believe so.

ServerScriptService.LaserScript:10: bad argument #1 (interval is empty)

Which line is line 10 in your LaserScript script?

local randomTile = tiles[math.random(1,#tiles)]

Try this:

for _, v in pairs(PathToThing:GetChildren()) do
    local random = math.random(1, #v)
-- rest of your code
end

With what I need the random tile for, this doesn’t fit it nor work with my code from my small testing I just did.

This is the error I was talking about. Try printing to debug, e.g.

print(#tiles) -- number > 0 expected
print(TileList:GetChildren()) -- table: address expected

Things like that will help narrow down the problem