@ErickShinjitsu Noted. I have changed code snippets iterating through arrays to ipairs
instead of pairs
. I should’ve done that in the first place and it was an oversight on my part (an oversight I’m afraid I’ve overlooked too many times; going to try and work on that personally!).
@achdef While not addressed to me, noted.
@QuantixDev I’ve never thought to asynchronously load assets or use coroutines in this instance, and I forgot you could use BindableEvents in such a fashion to replace Wait()
(in the instance provided here, anyway). I’ve incorporated some of your code into my script, and it’s worked for the most part.
However, I have ran into a slight issue. In the following snippet, the number of GUI objects being loaded (being 3) eventually are equivalent to the amount of objects indicated in Guiassets
. Although for some reason, this is not the case in Physicalassets
. Out of the 6 objects in Physicalassets
, in output it only says 4 were loaded.
It’s quite late at the moment (3:20 PM CST) and I think I’ve made an oversight somewhere that would cause the number of assets found in Physicalassets
to not equal workspaceinstancesinstancesLoaded
. I’m sure the assets are being loaded, though. Any insight into this bug would be appreciated. I’m going to attempt to go over the code again when I next wake up with a fresh mind and hopefully find it.
Code snippet:
-- BindableEvents to yield until each part is complete of the script
local LoadedGuiAssetsBE = Instance.new('BindableEvent')
local LoadedPhysicalAssetsBE = Instance.new('BindableEvent')
-- local plr, replicatedfirst service, preloadgui
local repfirst = game:GetService('ReplicatedFirst')
local lplr = game:GetService('Players').LocalPlayer
local preloadGui = script.Parent
-- service and audio
local ContentProviderService = game:GetService('ContentProvider')
local SoundToPlay = preloadGui:WaitForChild('sound')
-- gui elements
local BackgroundFrame = preloadGui:WaitForChild('BackgroundFrame')
local USMCLogoImage, IntroTextLabel, LoadingTextLabel = BackgroundFrame:WaitForChild('USMCLogoImage'), BackgroundFrame:WaitForChild('IntroTextLabel'), BackgroundFrame:WaitForChild('LoadingTextLabel')
-- workspace assets & instances loaded counter
local Physicalassets = {}
local Guiassets = {}
local workspaceinstancesinstancesLoaded = 0
local preloadguiinstancesloaded = 0
-- parenting to player and disabling default load screen
preloadGui.Parent = lplr.PlayerGui
repfirst:RemoveDefaultLoadingScreen()
-- loading GUI descendents first
local guidescendents = script.Parent:GetChildren()
for _, preGuiElement in ipairs(guidescendents) do
table.insert(Guiassets, preGuiElement)
end
--
for _, thingy in ipairs(Guiassets) do
print(tostring(thingy))
end
--
for _, addedGuiElement in ipairs(Guiassets) do
coroutine.wrap(
function()
ContentProviderService:PreloadAsync({addedGuiElement}, function()
preloadguiinstancesloaded = preloadguiinstancesloaded + 1
print("Gui Loading: ".. preloadguiinstancesloaded .. " / " .. #Guiassets )
if preloadguiinstancesloaded >= #Guiassets then LoadedGuiAssetsBE:Fire() end
end)
end
)()
end
if LoadedGuiAssetsBE ~= true then LoadedGuiAssetsBE.Event:Wait() end
print('Finished loading preload GUI! Moving to physical workspace assets!')
-- loading physical game objects
local workspaceDescendents = workspace:GetChildren()
for _, workspaceObj in ipairs(workspaceDescendents) do
if workspaceObj.ClassName == 'Part' or workspaceObj.ClassName == 'Model' or workspaceObj.ClassName == 'UnionOperation' then
table.insert(Physicalassets, workspaceObj)
end
end
for _, addedPhysicalObject in ipairs(Physicalassets) do
coroutine.wrap(
function()
ContentProviderService:PreloadAsync({addedPhysicalObject}, function()
workspaceinstancesinstancesLoaded = workspaceinstancesinstancesLoaded + 1
print("Workspace Loading: ".. workspaceinstancesinstancesLoaded .. " / " .. #Physicalassets )
if workspaceinstancesinstancesLoaded >= #Physicalassets then LoadedPhysicalAssetsBE:Fire() end
end)
end
)()
end
if LoadedPhysicalAssetsBE ~= true then LoadedPhysicalAssetsBE.Event:Wait() end
print('Finished loading preload GUI! Moving to physical workspace assets!')
LoadingTextLabel.Text = 'Done loading! Please enjoy your stay while in Parris Island. Remember to adhere to applicable law and regulations.'
wait(2)
script.Parent:Destroy()
Attached repro containing scripts & physical assets:
repro.rbxl (37.4 KB)
By the way, I recognize that this post delves more into another category within the Help and Feedback section, but I’m not sure if I should make a new post with this bug/issue or just leave it here. Let me know if I need to correct anything regarding this.
Thanks!