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.
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?
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
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
The print has nothing to do with this. I am concentrated on the second line.
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
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.