..Trying to make a cleanup script becuase explosions can sometimes wont get deleted..basically im making a magic game, and the projectiles explosions are all called booom and booom2. is there a way to get all of the booms and boom2s to get selected and get deleted at the same time.
1 | Deletables = game.Workspace:GetChildren( "Booom" ) |
2 | Deletables 2 = game.Workspace:GetChildren( "Booom2" ) |
3 |
4 | while true do |
5 | Wait(*For a reasonable time*) |
6 | Deletbles:Destroy() |
7 | Deletables 2 :Destroy() |
8 | end |
lol i am bad
Try this, and use the code blocks next time. look at the comments embedded.
01 | local function GetChildrenFromName(parent, name) |
02 | local t = { } |
03 | for _, obj in next , parent:GetChildren() do -- loop through parents children |
04 | if obj.Name = = name then -- check if the object's name is the name given |
05 | t [ #t+ 1 ] = obj -- add the object to table |
06 | end |
07 | end |
08 | return t -- return the table |
09 | end |
10 | -- new function created! |
11 | -- table GetChildrenFromName [function], Args #1 = Parent, Args #2 Name |
12 | local deletables = GetChildrenFromName(game.Workspace, "Booom" ) -- use our New function |
13 | local deletables 2 = GetChildrenFromName(game.Workspace, "Booom2" ) -- use the function again |
14 | while wait() do -- forever |
15 | -- you cant delete a table, loop through it! |
Hope this works in your context, please mark this as a solution if this works for you. Metatable out!
You would need to loop through the children in workspace and pick out the names from there, and you can do that with for loops.
Here is what the new script would look like:
01 | local Time = 60 -- The time you want it to disappear |
02 |
03 | while true do |
04 | for i, v in pairs (workspace:GetChildren()) do -- Getting all the children in workspace |
05 | if v.Name = = "Booom" or v.Name = = "Booom2" then -- Checking the names of children |
06 | v:Destroy() -- Destroying the booms |
07 | end |
08 | end |
09 | wait(Time) -- Wait to stop the loop from breaking the game. |
10 | end |
That should hopefully work.
To be honest, there is a better way to do this. Such as using Debris
. I can't see your magic script but I would guess that there is a part where you create a boom. Under that part you could write:
1 | game.Debris:AddItem(BoomName, AmountOfTimeUntilDisappear) |
That would work better, but if you cannot use that then stick with the fixed code I wrote.
Hope this helps!