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

How do I add the children of a model to a table, and then preload them?

Asked by
microk 0 Donator
9 years ago

I'm new to scripting, and I have a sound library (just a model with all the sounds in the whole game) that I want to preload when the game runs.

How do I get the contents of that model into a table, and most importantly how do I then preload them?

No, this is not a request, I can write Lua, but the wiki doesn't have much info on this. I also see some games that preload assets, and I've always wondered how.

I'd much rather preload them than to insert them all into a part (decals) or stop and play them (sounds).

Thanks.

0
I like how I get a negative rating for just not knowing something. 10/10 website. microk 0 — 9y
0
Granted, I can learn tables and such with the wiki, preloading is very unclear, and I've gotten no help with it. microk 0 — 9y
0
You'll get that sometimes. I believe that asking to know how to do something is okay. Spongocardo 1991 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

The different types of for loops:

You can use for loops to go through all the objects. You can either use numeric loops (for i = 1,10 do) or generic loops (for i,v in pairs(TABLE) do) to accomplish this.

Let's assume your sound library is in ServerStorage in a Model called SLibrary, we would either of the following pieces of code to get all the children.

Generic For:

for i,v in pairs(game.ServerStorage.SLibrary:GetChildren()) do --The GetChildren method returns all the children of an object in a table, so we can use a for loop to loop through each value (child) in the table. i is the index variable (position) and v is the value variable (object, number, etc.)
end --For loops require and end

Numeric For:

local library = game.ServerStorage.SLibrary:GetChildren() --I like to make a variable of the children table before doing the loop.
for i = 1, #library do --The # operator returns the number of items in the table. 3 children would make the # operator return the number 3.
end --For loops require an end

Preloading the sound:

Now to preload, we would do the following:

Generic For:

for i,sound in pairs(game.ServerStorage.SLibrary:GetChildren()) do --Generic for loop, looping through the returned table of the GetChildren() method.
    if sound:IsA("Sound") then --If the value variable is a sound then. You can use the IsA() method to get the type of object an instance is. Just a precaution in case there is something that isn't a sound in there.
        game:GetService("ContentProvider"):Preload(sound.SoundId) --Preload the sound id of the value variable we are currently looping through. The Preload method is found in the ContentProvider service, which can be accessed by either using the GetService() method or referencing it directly.
    end --End the if statement.
end --Ends the for loop.

Numeric For:

local library = game.ServerStorage.SLibrary:GetChildren() --Variable for the children table.
for i = 1, #library do --Numeric for loop through 1 and the number of items in the library variable's table.
    if library[i]:IsA("Sound") then --We don't have a value variable, so we have to reference the table and use i to point to the object we want.
        game:GetService("ContentProvider"):Preload(library[i].SoundId) --Mostly the same as the last piece of code, except we have to reference the table and use i to indicate the object we're looping through for the Preload() method's argument.
    end --End if statement.
end --End for loop.

The two pieces of code above are similar in some ways and different in others. It is important to notice these differences for when you use for loops in the future.

That should give you an idea of what to do, I hope this helped.

0
Please let me know if I've made any mistakes, I will correct them when I can. I checked most of it and it seems fine to me but some things could be off as I was sort of rushing as I have school tomorrow. Spongocardo 1991 — 9y
Ad

Answer this question