CCCPaste

Legends Of Speed - 8 - Data Script

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("_0")

local function OnPlayerJoin(Player)
	local UserId = Player.UserId
	local Key = "Player_" .. UserId

	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player

	local RedeemedCodes = Instance.new("Folder")
	RedeemedCodes.Name = "RedeemedCodes"
	RedeemedCodes.Parent = Player

	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Value = 0
	Gems.Parent = Player

	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = Player

	local RequiredXP = Instance.new("IntValue")
	RequiredXP.Name = "RequiredXP"
	RequiredXP.Value = 0
	RequiredXP.Parent = Player

	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = Player

	local Steps = Instance.new("IntValue")
	Steps.Name = "Steps"
	Steps.Value = 0
	Steps.Parent = Leaderstats

	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = Leaderstats

	local Races = Instance.new("IntValue")
	Races.Name = "Races"
	Races.Value = 0
	Races.Parent = Leaderstats

	local Hoops = Instance.new("IntValue")
	Hoops.Name = "Hoops"
	Hoops.Value = 0
	Hoops.Parent = Leaderstats

	local Success, SavedData = pcall(function()
		return DataStore:GetAsync(Key)
	end)

	if Success and SavedData then
		Gems.Value = SavedData.Gems or 0
		XP.Value = SavedData.XP or 0
		RequiredXP.Value = SavedData.RequiredXP or 0
		Level.Value = SavedData.Level or 1
		Steps.Value = SavedData.Steps or 0
		Rebirths.Value = SavedData.Rebirths or 0
		Races.Value = SavedData.Races or 0
		Hoops.Value = SavedData.Hoops or 0

		if SavedData.RedeemedCodes then
			for _, Code in ipairs(SavedData.RedeemedCodes) do
				local CodeInstance = Instance.new("StringValue")
				CodeInstance.Name = Code
				CodeInstance.Parent = RedeemedCodes
			end
		end
	else
		warn("Failed To Load Data For", Player.Name)
	end
end

local function OnPlayerLeave(Player)
	local UserId = Player.UserId
	local Key = "Player_" .. UserId

	local Leaderstats = Player:FindFirstChild("leaderstats")
	local RedeemedCodes = Player:FindFirstChild("RedeemedCodes")

	if Leaderstats and RedeemedCodes then
		local Gems = Player:FindFirstChild("Gems")
		local XP = Player:FindFirstChild("XP")
		local RequiredXP = Player:FindFirstChild("RequiredXP")
		local Level = Player:FindFirstChild("Level")

		local Steps = Leaderstats:FindFirstChild("Steps")
		local Rebirths = Leaderstats:FindFirstChild("Rebirths")
		local Races = Leaderstats:FindFirstChild("Races")
		local Hoops = Leaderstats:FindFirstChild("Hoops")

		if Gems and XP and RequiredXP and Level and Steps and Rebirths and Races and Hoops then
			local Data = {
				Gems = Gems.Value,
				XP = XP.Value,
				RequiredXP = RequiredXP.Value,
				Level = Level.Value,
				Steps = Steps.Value,
				Rebirths = Rebirths.Value,
				Races = Races.Value,
				Hoops = Hoops.Value,
				RedeemedCodes = {}
			}

			for _, CodeInstance in ipairs(RedeemedCodes:GetChildren()) do
				if CodeInstance:IsA("StringValue") then
					table.insert(Data.RedeemedCodes, CodeInstance.Name)
				end
			end

			local Success, Error = pcall(function()
				DataStore:SetAsync(Key, Data)
			end)

			if not Success then
				warn("Failed To Save Data For", Player.Name, "Error:", Error)
			end
		end
	end
end

Players.PlayerAdded:Connect(OnPlayerJoin)
Players.PlayerRemoving:Connect(OnPlayerLeave)

game:BindToClose(function()
	for _, Player in pairs(Players:GetPlayers()) do
		OnPlayerLeave(Player)
		Player:Kick("The server has shutdown!")
	end
end)