Datastore Logic

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService('ReplicatedStorage')

-- Week Number
local function GetCurrentWeekNumber()
	task.wait(1)
	local WeekNumber = game:GetService("ReplicatedStorage"):WaitForChild("GameData"):WaitForChild("WeekNumber").Value
	return WeekNumber
end

-- DataStores
local TotalXPDataStore = DataStoreService:GetOrderedDataStore("TotalXP")
-- WeeklyXPDataStore defined below on line 166 (so we can get current week number at the time players data is loaded)

local WinsDataStore = DataStoreService:GetOrderedDataStore("Wins")
local GoalsDataStore = DataStoreService:GetOrderedDataStore("Goals")
local KillsDataStore = DataStoreService:GetOrderedDataStore("Kills")
local CoinsDataStore = DataStoreService:GetOrderedDataStore("Coins")

local function saveStatsData(player)
	
	if game.PrivateServerId ~= "" then
		return
	end
	
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats == nil then warn("No leaderstats?") return end
	
	-- TotalXP
	if player:GetAttribute("DataLoadFail") ~= true then
		local TotalXPDataToSave = player.leaderstats.TotalXP.Value
		local TotalXpSuccsess, err = pcall(function()
			TotalXPDataStore:SetAsync(player.UserId, TotalXPDataToSave)
		end)
		if TotalXpSuccsess then 

		else 
			warn(err)		
		end
	else
		print("Not saving stats since data failed to load initially")
	end

	-- WeeklyXP
	if player:GetAttribute("DataLoadFail") ~= true then
		if player:GetAttribute("StartWeek") == GetCurrentWeekNumber() then
			local WeeklyXPDataToSave = leaderstats.WeeklyXP.Value
			local WeeklyXpSuccsess, err = pcall(function()
				DataStoreService:GetOrderedDataStore("WeeklyXP" .. GetCurrentWeekNumber()):SetAsync(player.UserId, WeeklyXPDataToSave)
			end)
			if WeeklyXpSuccsess then
			else
				warn(err)
			end
		else
			warn("Reseting players weekly data because these are stats from last week!")
		end
	end

	-- Wins
	if player:GetAttribute("DataLoadFail") ~= true then
		local WinsDataToSave = leaderstats.Wins.Value
		local WinSuccess, err = pcall(function()
			WinsDataStore:SetAsync(player.UserId, WinsDataToSave)
		end)
		if WinSuccess then 

		else 
			warn(err)		
		end
	end

	-- Goals
	if player:GetAttribute("DataLoadFail") ~= true then
		local GoalsDataToSave = leaderstats.Goals.Value
		local GoalSuccess, err = pcall(function()
			GoalsDataStore:SetAsync(player.UserId, GoalsDataToSave)
		end)
		if GoalSuccess then 

		else 
			warn(err)		
		end
	end

	-- Kills
	if player:GetAttribute("DataLoadFail") ~= true then
		local KillsDataToSave = leaderstats.Kills.Value
		local KillsSuccess, err = pcall(function()
			KillsDataStore:SetAsync(player.UserId, KillsDataToSave)
		end)
		if KillsSuccess then 

		else 
			warn(err)		
		end	
	end

	-- Coins
	local CoinsToSave = leaderstats:FindFirstChild("Coins")
	local CoinsDataToSave = CoinsToSave.Value
	local CoinsSuccess, err = pcall(function()
		CoinsDataStore:SetAsync(player.UserId, CoinsDataToSave)
	end)
	if CoinsSuccess then 

	else 
		warn(err)		
	end	
end

Players.PlayerAdded:Connect(function(player) 

	-- Create the leaderstats folder
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local TotalXP = Instance.new("IntValue")
	TotalXP.Name = "TotalXP"
	TotalXP.Parent = leaderstats

	local WeeklyXP = Instance.new("IntValue")
	WeeklyXP.Name = "WeeklyXP"
	WeeklyXP.Parent = leaderstats

	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats

	local Goals = Instance.new("IntValue")
	Goals.Name = "Goals"
	Goals.Parent = leaderstats

	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Parent = leaderstats

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats

	-- Load TotalXPData
	local TotalXPData = nil
	local TotalXPSuccsess, err = pcall(function()
		TotalXPData = TotalXPDataStore:GetAsync(player.UserId) 
	end)

	if TotalXPSuccsess and TotalXPData then 
		TotalXP.Value = TotalXPData
	elseif TotalXPSuccsess then 

	else
		warn("TotalXP Data Loading Error:" .. err)
		player:SetAttribute("DataLoadFail", true)
	end

	-- Load WeeklyXPData
	player:SetAttribute("StartWeek", GetCurrentWeekNumber())

	local WeeklyXPData = nil
	local WeeklyXPSuccsess, err = pcall(function()
		WeeklyXPData = DataStoreService:GetOrderedDataStore("WeeklyXP" .. GetCurrentWeekNumber()):GetAsync(player.UserId) 
	end)

	if WeeklyXPSuccsess and WeeklyXPData then 
		WeeklyXP.Value = WeeklyXPData	
	elseif WeeklyXPSuccsess then 

	else
		warn("WeeklyXP Data Loading Error:" .. err)
		player:SetAttribute("DataLoadFail", true)
	end

	-- Load WinsData
	local WinsData = nil
	local WinsSuccess, err = pcall(function()
		WinsData = WinsDataStore:GetAsync(player.UserId)
	end)

	if WinsSuccess and WinsData then
		Wins.Value = WinsData
	elseif WinsSuccess then

	else
		warn("Wins Data Loading Error:" .. err)
		player:SetAttribute("DataLoadFail", true)
	end

	-- Load GoalsData
	local GoalsData = nil
	local GoalsSuccess, err = pcall(function()
		GoalsData = GoalsDataStore:GetAsync(player.UserId)
	end)

	if GoalsSuccess and GoalsData then
		Goals.Value = GoalsData
	elseif GoalsSuccess then

	else
		warn("Goals Data Loading Error:" .. err)
		player:SetAttribute("DataLoadFail", true)
	end

	-- Load KillsData
	local KillsData = nil
	local KillsSuccess, err = pcall(function()
		KillsData = KillsDataStore:GetAsync(player.UserId)
	end)

	if KillsSuccess and KillsData then
		Kills.Value = KillsData
	elseif KillsSuccess then

	else
		warn("Kills Data Loading Error:" .. err)
		player:SetAttribute("DataLoadFail", true)
	end

	-- Load CoinsData
	local CoinsData = nil
	local CoinsSuccess, err = pcall(function()
		CoinsData = CoinsDataStore:GetAsync(player.UserId)
	end)

	if CoinsSuccess and CoinsData then
		Coins.Value = CoinsData
	elseif CoinsSuccess then

	else
		warn("Coins Data Loading Error (Kicking Player):" .. err)
		-- player:Kick("Data loading error - check your connection & Roblox status")
	end
end)

Players.PlayerRemoving:Connect(function(player) 
	saveStatsData(player)
end)

game:BindToClose(function() 
	for _, player in pairs(Players:GetPlayers()) do 
		local success, err  = pcall(function()
			saveStatsData(player)
		end)
	end
end)

while true do
	task.wait(100)
	for i, player in pairs(Players:GetPlayers()) do
		saveStatsData(player)
	end
end
3 Likes