Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to clone a model multiple times efficiently?

Asked by 6 years ago

So currently I am working on a simple map system and am using clone() to replicate a model multiple times to be setup on the map. But it seems tedious to have to clone the model multiple times and set a name to it as well. Is there a way to setup models and be able to call them back later without having to set them up myself?

This is what i'm currently doing

local fenceCloneA = terrain_list["TopFence"]:Clone()
local fenceCloneB = terrain_list["TopFence"]:Clone()
local fenceCloneC = terrain_list["TopFence"]:Clone()
local fenceCloneD = terrain_list["TopFence"]:Clone()
local fenceCloneE = terrain_list["TopFence"]:Clone()
local fenceCloneF = terrain_list["TopFence"]:Clone()

fenceCloneA.Parent = workspace
fenceCloneB.Parent = workspace
fenceCloneC.Parent = workspace
fenceCloneD.Parent = workspace
fenceCloneE.Parent = workspace
fenceCloneF.Parent = workspace

Is there an easier way to be able to clone these and be able to look back at them later if needed?

0
Use a loop Azarth 3141 — 6y
0
How would I use a loop in this case? Do I use a for loop to repeatedly clone it then parent it to the workspace, if so how would I go around to do it. Korokozo 2 — 6y

2 answers

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago
local function clone_something(thing, howmanytimes)
    local things_tab = {}
    for i = 1, howmanytimes do 
        table.insert(things_tab, thing:Clone())
    end
    return things_tab
end


local things = clone_something(terrain_list["TopFence"], 3)
for i,v in pairs(things) do 
    v.Parent = workspace
end

Ad
Log in to vote
0
Answered by 6 years ago

Put them in a folder. That way, you only have to clone one folder.

0
That would work for when I know how many I need, but what about when the number needed is unknown or when it is less than what is needed. If I put it in the folder the amount I have will be finite and I would have either less or more than needed at some points Korokozo 2 — 6y
0
Just use GetChildren() then KennySfromTitan 106 — 6y

Answer this question