Studio sometimes fails to load leaderstats

I’m not sure why this is happening but studio sometimes fails to load leaderstats:

Script:

local DataStoreService = game:GetService("DataStoreService")
local DataStoreModule = require(1936396537) -- Datastore 2
local BadgeService = game:GetService("BadgeService")

-- Define the data keys and their default values
local dataKeys = {
	-- leaderstats
	{name = "Coins", defaultValue = 0, dataType = "IntValue", parent = "leaderstats"},
	{name = "Wins", defaultValue = 0, dataType = "IntValue", parent = "leaderstats"},
	-- Settings
	{name = "Music", defaultValue = true, dataType = "BoolValue", parent = "data"},
	-- data
	{name = "InRound", defaultValue = false, dataType = "BoolValue", parent = "data"},
}

-- Combine the keys in DataStoreModule
local keys = {}
for _, data in ipairs(dataKeys) do
	table.insert(keys, data.name)
end
DataStoreModule.Combine("MasterKey", unpack(keys))

-- Helper function to create value instances for leaderstats or data folder
local function createValueInstance(data, parent)
	local valueInstance = Instance.new(data.dataType)
	valueInstance.Name = data.name:gsub("^%l", string.upper)
	valueInstance.Parent = parent
	valueInstance.Value = data.defaultValue -- Set default value initially
	return valueInstance
end

-- Function to load player data and connect it for live saving
local function loadDataAndConnect(data, player, valueInstance)
	local dataStore = DataStoreModule(data.name, player)
	local success, storedValue = pcall(function()
		return dataStore:Get()
	end)

	-- Set the value to either the stored value or the default if unavailable
	if success then
		valueInstance.Value = storedValue ~= nil and storedValue or data.defaultValue
	else
		warn("Failed to load data for", data.name, "for player", player.Name)
	end

	-- Save updated values on change with retry handling
	valueInstance:GetPropertyChangedSignal("Value"):Connect(function()
		local retries = 3
		while retries > 0 do
			local success, errorMsg = pcall(function()
				dataStore:Set(valueInstance.Value)
			end)
			if success then break end
			retries -= 1
			if retries == 0 then
				warn("Failed to save data for", data.name, "for player", player.Name, ":", errorMsg)
			else
				wait(2) -- Wait before retrying to handle throttling
			end
		end
	end)
end

-- Player added function to initialize leaderstats and data folders
game.Players.PlayerAdded:Connect(function(player)
	local leaderstatsFolder = Instance.new("Folder", player)
	leaderstatsFolder.Name = "leaderstats"

	local dataFolder = Instance.new("Folder", player)
	dataFolder.Name = "data"

	for _, data in ipairs(dataKeys) do
		local parentFolder = data.parent == "leaderstats" and leaderstatsFolder or dataFolder
		local valueInstance = createValueInstance(data, parentFolder)
		loadDataAndConnect(data, player, valueInstance)
	end
end)

-- Save data when player leaves
game.Players.PlayerRemoving:Connect(function(player)
	for _, data in ipairs(dataKeys) do
		local dataStore = DataStoreModule(data.name, player)
		local valueInstance = player:FindFirstChild(data.parent):FindFirstChild(data.name)
		if valueInstance then
			local retries = 3
			while retries > 0 do
				local success, errorMsg = pcall(function()
					dataStore:Set(valueInstance.Value)
				end)
				if success then break end
				retries -= 1
				if retries == 0 then
					warn("Failed to save data for", data.name, "for player", player.Name, "on removal:", errorMsg)
				else
					wait(2)
				end
			end
		end
	end
end)

PlayerAdded is known for not always firing inside studio due to the Script reaching that point after the Player Instance is created.

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