How to properly wait for data to load

I have a data module that adds the “DataLoaded” attribute to a player when he is loaded.

local function PlayerAdded(Player : Player)
	local Profile = ProfileStore:LoadProfileAsync(DataEncode.."-"..Player.UserId)
	if Profile then
		-- Main
		Profile:AddUserId(Player.UserId)
		Profile:Reconcile()

		-- Clear Player
		Profile:ListenToRelease(function()
			Profiles[Player] = nil
			Player:Kick()
		end)

		-- Sanity Check
		if not Player:IsDescendantOf(Players) then
			Profile:Release()
		else
			Profiles[Player] = Profile
			Player:SetAttribute("DataLoaded", true)
			Remotes.Data.DataLoaded:FireClient(Player, DataModule:Get(Player, "Full"))
			print("Loaded ".. Player.Name.. "'s Data Successfully")
		end
	else
		Player:Kick()
	end
end

Already on the client script I wait for this attribute and only then do my things in it

repeat
	task.wait(.1)
until Player:GetAttribute("DataLoaded")

I’m not sure that’s a good decision, are there any better options?

Try Player:GetAttributeChangedSignal("DataLoaded"):Wait(); it will wait for it to change instead of constantly checking if it has changed.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.