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

How to reference all of one type of asset in a parent?

Asked by 5 years ago

Im making a script that will be referencing a brick that includes many, many audios. I know theres a way to reference everything thats an audio, but I have no clue how to write it. I dont know if this counts as a request so I apologize... but how would i rewrite this? All I want is for the script to get every child of a certain set parent that is an audio, and stop it from playing.

local b = game.Workspace.musicbrick

b.audio1:Stop()
b.audio2:Stop()
b.audio3:Stop()
b.audio4:Stop()
b.audio5:Stop()
b.audio6:Stop()
b.audio7:Stop()

2 answers

Log in to vote
-1
Answered by 5 years ago

So you'd have to loop through the sounds and check whether the current child is a sound or not by using :IsA("Sound"). So in the script you'd write this:

for i,v in pairs(game.Workspace.musicbrick:GetChildren()) do -- Loops through the children
    if v:IsA("Sound") then -- Checks whether it's a sound or not
        v:Stop() -- Stops the sound
    end
end
0
thank you! this is perfect :) Octocentillion 15 — 5y
Ad
Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago
Edited 5 years ago

For this you gotta use tables! A table (or you can call it an array) is pretty much a variable that can store multiple values, and such as variables tables can have different types of values (integer, string, boolean, number.....). For more information A table is assigned like this t = {1, true, "runs"} its like that, it stores different values, and to get those values you gotta do for example t[the index of the value] so for example t[3] is "runs" in the table we assigned earlier, now after storing these audio objects in our table, it will be boring to mention them one by one, so we gotta loop tem, and loop te memebers of a table we gotta use an in pairs loop. I probarly not gonna be good at explaining this so here you go

local b = game.Workspace.musicbrick
local audios = {b.audio1,
b.audio2,
b.audio3,
b.audio4,
b.audio5,
b.audio6,
b.audio7}
--this is the table we assigned

for i, v in pairs(audios) do
-- this is how an in pairs loop look like, v is gonna be the table's member that is looping each time
    v:Stop()
end


Now we can make this way efficent by doing

for i,v in pairs(game.Workspace.musicbrick:GetChildren()) do 
    if v:IsA("Sound") then -- Checks whether if the given object is a certain classname, so it sees if v is a sound
        v:Stop()
    end
end

And that's it! sorryf or the poor explanation, i got a perfect explantion here

0
and you can follow what despicable did which is way efficent, i just gave you an idea of what ur doing starmaq 1290 — 5y
0
you ignored me ;( starmaq 1290 — 5y
0
ty :> starmaq 1290 — 5y
0
did you down vote him? why xd starmaq 1290 — 5y

Answer this question