Hey Everyone!
First of all, I have 2 problems. 1, is that “loaded” Isn’t printing, secondly, is how can I display what’s loaded as a percentage?
Here is my script:
local Gui = script.Parent
local Background = Gui.Background
local Percentage = Background.PercentLabel
local ItemsToLoad = {}
for i, Item in pairs(game.Workspace:GetDescendants()) do
table.insert(ItemsToLoad,Item)
print('Inserted '..Item.Name..' into the ItemsToLoad table!')
end
wait(.5)
local MathDivision = #ItemsToLoad
for i, Item in ipairs(ItemsToLoad) do
if game.Workspace:FindFirstAncestor(Item) ~= nil then
if not game.Workspace:FindFirstAncestor(Item):IsLoaded() then
repeat wait() until game.Workspace:FindFirstAncestor(Item):IsLoaded()
print('Loaded'..Item)
end
end
end
To clarify it only yields until the descendants of every object in the table passed to ContentProvider:PreloadAsync() as an argument have replicated for every client, it does not preload the entire game.
Basically loading items would require PreloadAsync() but you can make it a pseudo loading screen as of right now, as for the percentage, normally in shops there would be a 15% discount or whatever, basically if the item value was 100$ then - 15% would create this formula: 100-(100*(15/100)) basically stating 100 - (100*(#ItemsLoaded/100)) so saying if 50 was the amount of stuff loaded, 100 - (100*(50/100)), there would be 50% remaining.
To simplify just 100(#itemsLoaded/#itemsToLoad) which through one way of doing it, could be through either ContentProvider.RequestQueueSize to get the number of items to download for a client (not a good way of going about this) or via the number of elements (objects to load) in a table (taking into account their descendants manually), after through the callback of PreloadAsync() incrementing the number by one (indicating one item has loaded) and expressing the ratio loaded/TotalItemsToLoad as a percentage.
You can use a table as the first argument for :PreloadAsync, no need for repeat loops.
Here’s my implementation:
function LoadData(v,func)
assert(v, "[LOAD]: Expected first argument to be a table or instance")
if typeof(v) == "Instance" then
v = v:GetChildren()
end
local Time = tick()
local succes, err = pcall(function()
Content:PreloadAsync(v,func)
end)
if err then print(err) end -- print if there is an error
print("[LOAD]: Finished! Time: " .. tostring(string.sub(tick() - Time,1,5)))
end
Call the function with a table or instance, and it should preload it.
For the loading screen for my game I have had issues with preloading an entire table of assets. It loaded them but the increment wasn’t correct with the callback. When I tried this out it wouldn’t count more than 200 assets. To solve this I used preloadasync on each asset to load instead of putting it in a table and that fixed it for me. There might be other methods that I haven’t tried out yet though.