How can you store multiple things in workspace that have the same name in an array?
How can you store multiple things in workspace that have the same name in an array?
2 answers
myTable = {} --Your table.
desiredName = "YourDesiredName" --Your desired name.
for i,v in pairs(game.Workspace:GetChildren()) do --Set up for loop for Workspace's childre.
if v.Name == desiredName then --Check if the name is the same.
table.insert(v, myTable) --Insert the instance if the name is the same.
end
end
If I helped you out, make sure to hit the Accept Answer button below my character! :D
1 comment
thanks so much!
Your question is pretty broad.. but i'll try to help. You should definitely elaborate on what you want.
If you mean that you have an array full of names, and if there's an object in the workspace with a name that matches a name in the array then parent it somewhere else then..
First, you should define the array
local names = {"Thing","Thing1","Thing2","Thing3"}
Then you should iterate through the table, and use an if statement to check if there's a part with the same name in workspace.
for i,v in pairs(names) do
local obj = workspace:FindFirstChild(v)
if obj then
And lastly you should store it by parenting it to wherever you want. Here's it all together.
local names = {"Thing","Thing1","Thing2","Thing3"}
for i,v in pairs(names) do
local obj = workspace:FindFirstChild(v)
if obj then
obj.Parent = --Put the parent here
end
end
1 comment
yes, that would work as well, I like the script discern shared better though, its exactly what I need and he posted before you, but I gave you a rep point for the help
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Can you explain why you would want to do that, and what do you mean by "array" and "name"?
I have multiple ammo tables in a map, I want to store them on an array inside a script which is inside the gun and I want to cycle through them to check if one is within range of the player