Shagabash
|
  |
| Joined: 20 Jun 2008 |
| Total Posts: 6217 |
|
|
| 30 Dec 2011 08:20 PM |
Hi, I'm an amateur scripter, lol.
I'm familiar with basic functions like :remove()
But, how do I remove all of something? For example I'm going to have a bunch of balls that are named "Ball". I want them to be all removed at once.
This is probably really easy, but, I didn't find it on the wiki (maybe didn't look hard enough).
Help? |
|
|
| Report Abuse |
|
|
Biostream
|
  |
| Joined: 28 Mar 2011 |
| Total Posts: 913 |
|
|
| 30 Dec 2011 08:21 PM |
for i, v in pairs(Workspace:GetChildren()) do if v.Name=="Ball" then v:remove() end end |
|
|
| Report Abuse |
|
|
Xsitsu
|
  |
| Joined: 28 Jul 2009 |
| Total Posts: 2921 |
|
|
| 30 Dec 2011 08:21 PM |
for _, v in pairs(game.Workspace:GetChildren()) do if v.Name == "Ball" then v:Destroy() end end
:Destroy() is better than remove.
This will destroy all objects in the workspace that are named Ball |
|
|
| Report Abuse |
|
|
AntiFiter
|
  |
| Joined: 14 May 2009 |
| Total Posts: 12290 |
|
|
| 30 Dec 2011 08:21 PM |
@Bio I don't get that. What does i, and v represent? |
|
|
| Report Abuse |
|
|
Xsitsu
|
  |
| Joined: 28 Jul 2009 |
| Total Posts: 2921 |
|
|
| 30 Dec 2011 08:23 PM |
@Bio
i is the first loop variable representing the number the loop is on. v is the object the loop is currently "looking at", a little hard to describe.
They can be named anything. |
|
|
| Report Abuse |
|
|
Xsitsu
|
  |
| Joined: 28 Jul 2009 |
| Total Posts: 2921 |
|
|
| 30 Dec 2011 08:24 PM |
@Me
You're stupid, you replied to the guy that the guy you were trying to reply to was replying to. Next time think things out more! |
|
|
| Report Abuse |
|
|
Shagabash
|
  |
| Joined: 20 Jun 2008 |
| Total Posts: 6217 |
|
|
| 30 Dec 2011 08:24 PM |
Thanks, it worked.
Also.. might I ask why :remove() isn't as good as :Destroy()? |
|
|
| Report Abuse |
|
|
|
| 30 Dec 2011 08:24 PM |
| You would have to say it as a Variable than. |
|
|
| Report Abuse |
|
|
Biostream
|
  |
| Joined: 28 Mar 2011 |
| Total Posts: 913 |
|
|
| 30 Dec 2011 08:25 PM |
In the pairs loop, the first argument is the index. Every time it reiterates(if that's a word) it will add one to it. V is the item in the list under the index i. So, this would print the value of i. for i, v in pairs(game.Workspace:GetChildren()) do print(i) end
If there are four children of workspace, you would get this in your output. 1 2 3 4
|
|
|
| Report Abuse |
|
|
Biostream
|
  |
| Joined: 28 Mar 2011 |
| Total Posts: 913 |
|
|
| 30 Dec 2011 08:26 PM |
| They don't have to be i and v. |
|
|
| Report Abuse |
|
|
AntiFiter
|
  |
| Joined: 14 May 2009 |
| Total Posts: 12290 |
|
|
| 30 Dec 2011 08:27 PM |
| But where does i v get their values? |
|
|
| Report Abuse |
|
|
Xsitsu
|
  |
| Joined: 28 Jul 2009 |
| Total Posts: 2921 |
|
|
| 30 Dec 2011 08:29 PM |
What Destroy does:
Sets the objects parent to nil Locks the objects parent Frees up the memory taken by the object Calls Destroy on all objects inside of the object
What Remove does:
Sets the objects parent to nil Calls Remove on all objects inside of the object
You can also do:
object.Parent = nil
If you want to remove the object for a little bit so you can bring it back later in tact the way it left. |
|
|
| Report Abuse |
|
|
Biostream
|
  |
| Joined: 28 Mar 2011 |
| Total Posts: 913 |
|
|
| 30 Dec 2011 08:32 PM |
When using a pairs() loop, you have to specify the list to iterate through, and the variable that the current object will be named, and the number that will be used as the index. i, which is normally the first argument, will be the index. The index always starts at one and adds one to itself every time. V is the object in the list under i index.
numbers={4, 5, 6}--this is my list
for i, v in pairs(numbers) do--you must specify the list, and two variables print(i)--i is the index, so you will get 1, 2, 3 print(v)--v is the object in the list under the index of i, so the 1st object in the list numbers is 4, the 2nd is 5, and the 3rd is six, so the output will be 4, 5, 6 |
|
|
| Report Abuse |
|
|