Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

is there a way to get the children of the workspace that has the same name?

Asked by 2 months ago
Edited by raid6n 2 months ago

..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.

here is what i have, its probably bad...

Deletables = game.Workspace:GetChildren("Booom")
Deletables2 = game.Workspace:GetChildren("Booom2")

while true do
    Wait(*For a reasonable time*)
    Deletbles:Destroy()
    Deletables2:Destroy()
end

lol i am bad

0
bruh my script is wierd CrazyCats84 58 — 2mo

2 answers

Log in to vote
1
Answered by 2 months ago

Try this, and use the code blocks next time. look at the comments embedded.

local function GetChildrenFromName(parent, name)
     local t = {}
     for _, obj in next, parent:GetChildren() do -- loop through parents children
          if obj.Name == name then -- check if the object's name is the name given
               t[#t+1] = obj -- add the object to table
          end
     end
     return t -- return the table
end
-- new function created!
-- table GetChildrenFromName [function], Args #1 = Parent, Args #2 Name
local deletables = GetChildrenFromName(game.Workspace, "Booom") -- use our New function
local deletables2 = GetChildrenFromName(game.Workspace, "Booom2") -- use the function again
while wait() do -- forever
-- you cant delete a table, loop through it!
     for _, o in next, deletables do -- loop through the deletables
          o:Destroy() -- delete the object, as you used last time on your original script.
     end
     for _, o in next, deletables2 do -- loop through the deletables2
          o:Destroy() -- delete the object, as you used last time on your original script
     end
end

Hope this works in your context, please mark this as a solution if this works for you. Metatable out!

0
Thank You CrazyCats84 58 — 2mo
Ad
Log in to vote
0
Answered by
Gokhaii 58
2 months ago

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:


local Time = 60 -- The time you want it to disappear while true do for i, v in pairs(workspace:GetChildren()) do -- Getting all the children in workspace if v.Name == "Booom" or v.Name == "Booom2" then -- Checking the names of children v:Destroy() -- Destroying the booms end end wait(Time) -- Wait to stop the loop from breaking the game. 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:

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!

0
thanks CrazyCats84 58 — 2mo
0
Wait, Does this work on models too? CrazyCats84 58 — 2mo
0
Anything you need to find inside of the workspace. Gokhaii 58 — 2mo

Answer this question