Question about randomly selecting an object from a table

My brain works logically, just like many others do, I suppose. But it has a hard time understanding a little easy code snippet that should be easy to understand, yet it’s not.

local kiddos = workspace:GetChildren()
local kiddo = kiddos[math.random(1,#kiddos)]
print(kiddo)
--[[

Oh boy, let's do this.

So as far as I can understand, the following should happen:
--]]
local kiddo = -- defining the variable
...= kiddos[] -- trying to find and return the Instance
[math.random] -- randomizes the objects
(1,...) -- randomizer starts at 1
(...,#kiddos) -- # should count and return the number of how many objects there are

But how does it find the instance name and return it? There is no such thing as string there.

If anyone would like to explain this better than “it’s just returning it as it was programmed to” then I would love that.

The script is weirdly spaced out for some reason. Maybe that’s why you can’t understand it?

What do you mean by

?


This is what it looks like on my screen. I pasted the script into studio and it errored immediately.

That is just what my brain understands what is happening over there. I explain what I know, it should not be used.

Wait are you asking for an explanation of this snippet?
image

kiddo should be math.random(1, #kiddos)

it shouldn’t be able to print the name if it’s printing a instance, to do that it would need to do print(kiddo.Name) , are you saying this script prints the name of a instance without indexing the Name property?

Oh ok. Basically, the built in print function automatically casts the inputs to strings, which in the case of instances, means printing the name.

well the first line gets the children of workspace and puts them in a table

the second line looks for a key in the kiddos table, this key is a random number between 1 and how many children are in the table, so this basically gets a random value in the table

the third line just prints that random value

I am asking how does this get the instance with no proper name involved. It cannot select an object from numbers, right?


A possible explanation. It gets a random number between the 2 ones, and then it selects and returns the instance on the chosen integer. That explains it :smiley:


The print has nothing to do with this. I am concentrated on the second line.

it should show the name in output either way

Even if it sometimes makes no sense at all, I just love Luau :grinning_face_with_smiling_eyes:

to index the instances children by their name you would do instance[name], but :GetChildren() returns a table containing each instance, but each key in that table is a number, so if you iterated through it and printed each index they would probably all be numbers

1 Like

That is another explanation for my issue, and wasn’t thinking of that. Pretty sure GetChildren, GetDescendants works exactly like a normal table.

local tabler = {"table","r"}

Reading it again and I realised what you were trying to ask now, essentially this is how it works.
The first line returns an array of the children of workspace.
The second line gets the the key by starting with one and ending with the array’s length.
The third line prints the key’s value.

image

print(workspace.SpawnLocation)

proof you don’t need the name property

Expected to print Instance data, such as internal data or something. Oh well, Instance.Name should still be used, or I suppose you can’t just

local name = tostring(workspace.SpawnLocation)

image

print(tostring(workspace.SpawnLocation) == workspace.SpawnLocation.Name)

actually you can do that