Could someone explain how this works?

So I have a folder in ReplicatedStorage called “Vehicles” with three models inside of it and I want to get those models and clone them into the workspace so I do:

local Cars = game:GetService("ReplicatedStorage").Vehicles:GetChildren():Clone()
Cars.Parent = workspace

But this throws me an error:
"Attempt to call a nil value"

So if I instead do this:

local Cars = game:GetService("ReplicatedStorage").Vehicles:GetChildren()
for _,obj in pairs(Cars) do
local Cloned = obj:Clone()
Cloned.Parent = workspace

Then it works perfectly fine, why is calling :Clone directly after :GetChildren() resulting in an error?

Thanks in advance!

1 Like

You cannot clone a table as if it is an Instance.

2 Likes

The error occurs because calling :GetChildren() on an Instance will return an array / table, which is not something that can currently be placed into the physical space of the world nor a specific Service/Instance (*without JSONEncoding it into a string that is stored in an Attribute, from my understanding).

The second codeblock is referencing each individual Instance inside of the table which is able to be moved around in the game.

2 Likes

GetChildren() returns a table of the children of an instance

so if you had a car, boat and a plane it would return

{
  boat = Vechiles.boat,
  car = Vechiles.car,
  plane = Vechiles.plane
}

Now you can only :Clone() an instance not a table so what for i, v does is it loops through each value in the table gets the Instance of that value and clones it seperately

4 Likes