Wait for persistent models to load similar to game:IsLoaded()

Is there a better way to handle PersistentLoaded, similar to this

if not game:IsLoaded() then
	game.Loaded:Wait()
end

unfortunately, PersistentLoaded is an event. I can’t find any function that checks if IsPersistentLoaded()

2 Likes

create a workspace attribute that will be set to true on persistent loaded

Unlike game.Loaded, the workspace.PersistentLoaded event is intended for the server and is fired for each player. There wouldn’t be a situation where workspace.PersistentLoaded:Wait() is used since that would only yield the script for a single player and not each player individually. The following however is a script that implements PersistentLoaded with a timeout.

local Players = game:GetService("Players")
local PlayersTable = {}
local Timer = 60 --How long the server should wait for "PersistentLoaded" to be fired for each player.

local function OnPlayerAdded(Player)
	PlayersTable[Player] = os.clock()
end

local function OnPlayerRemoving(Player)
	PlayersTable[Player] = nil
end

local function OnPersistentLoaded(Player, DidTimeout)
	PlayersTable[Player] = nil
	if DidTimeout then
		--PersistentLoaded" did not fire for the player.
	else
		--"PersistentLoaded" fired for the player.
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
workspace.PersistentLoaded:Connect(OnPersistentLoaded)

while true do
	task.wait()
	for Player, Time in pairs(PlayersTable) do
		if os.clock() - Time > Timer then
			OnPersistentLoaded(Player, true)
		end
	end
end