Load Data on when player joins (DataStore2)

How can I load players Data when they first join, so it can show the players currency. Because I don’t want the player to think they have had their data lost.

Here is my script:

(Most of the Script I want to focus on is in the :PlayerAdded event. Look there, instead of the whole script.)

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local MareketPlaceService = game:GetService("MarketplaceService")

local DataStore2 = require(ServerScriptService.DataStore2)

-- Combine every key you use. This will eventually be the default, but for now read the "Gotchas" section to understand why we need this.
DataStore2.Combine("DATA", "points" , "rubys" , "playTime" , "deaths" , "kills", "wins")

local defaultLevel = 1
-- local defaultRequiredXP = 100

local Products_Table_Points = {
	ID1 = 1266973724,
	ID2 = 1266973816,
	ID3 = 1266974175,
	ID4 = 1266974226,
	ID5 = 1266974333,
	ID6 = 1266991287,
}

local Products_Table_Rubys = {
	ID1 = 1266497790,
	ID2 = 1266497819,
	ID3 = 1266497925,
	ID4 = 1266497973,
	ID5 = 1266498009,
	ID6 = 1266498043,
}

local Prize_Table_Points = {
	Prize1 = 100,
	Prize2 = 500,
	Prize3 = 1000,
	Prize4 = 5000,
	Prize5 = 10000,
	Prize6 = 15000,
}
local Prize_Table_Rubys = {
	Prize1 = 50,
	Prize2 = 100,
	Prize3 = 250,
	Prize4 = 500,
	Prize5 = 750,
	Prize6 = 1000,
}

Players.PlayerAdded:Connect(function(player)
	-- get players data
	
	local pointsStore = DataStore2("points", player)
	local rubysStore = DataStore2("rubys", player)
	local playTimeStore = DataStore2("playTime", player)
	local deathsStore = DataStore2("deaths", player)
	local killsStore = DataStore2("kills", player)
	local winsStore = DataStore2("wins", player)
	
	-- instance into player's data

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

	local points = Instance.new("NumberValue")
	points.Name = "Points"
	points.Value = pointsStore:Get(0) -- The "0" means that by default, they'll have 0 points
	points.Parent = leaderstats
	
	local Rubys = Instance.new("IntValue")
	Rubys.Name = "Rubys"
	points.Value = rubysStore:Get(0) -- The "0" means that by default, they'll have 0 points
	Rubys.Parent = leaderstats

	local playTime = Instance.new("IntValue")
	playTime.Name = "playTime"
	points.Value = playTimeStore:Get(0) -- The "0" means that by default, they'll have 0 points
	playTime.Parent = leaderstats

	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	points.Value = deathsStore:Get(0) -- The "0" means that by default, they'll have 0 points
	Deaths.Parent = leaderstats

	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	points.Value = killsStore:Get(0) -- The "0" means that by default, they'll have 0 points
	Kills.Parent = leaderstats

	local roundKills = Instance.new("IntValue", player)
	roundKills.Name = "RoundKills"

	local Wins = Instance.new("IntValue")
	points.Value = winsStore:Get(0) -- The "0" means that by default, they'll have 0 points
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	-- Set the values Data, if the player has any
	
	pointsStore:OnUpdate(function(newPoints)
		-- This function runs every time the value inside the data store changes.
		points.Value = newPoints
	end)
	
	rubysStore:OnUpdate(function(newRubys)
		Rubys.Value = newRubys
	end)
	
	playTimeStore:OnUpdate(function(myplayTime)
		playTime.Value = myplayTime
	end)
	
	deathsStore:OnUpdate(function(newDeaths)
		Deaths.Value = newDeaths
	end)
	
	killsStore:OnUpdate(function(newKills)
		Kills.Value = newKills
	end)
	
	winsStore:OnUpdate(function(newWins)
		Wins.Value = newWins
	end)
	
	player.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid", 2)
		if humanoid then
			humanoid.Died:Connect(function()
				deathsStore:Increment(1)

				local tag = humanoid:WaitForChild("creator", 2)
				if not tag then
				--[[ We could construct a new object to put in the humanoid here, 
				but will error and return here as we can't continue. ]]
					error("Failed to find "..tag.." object in the Humanoid.")
					return
				end

			--[[ This was returning a string here, we need to get the player object of the killer!
			We can also use :FindFirstChild() here as the player should almost definitely exist here.]]
				local killer = Players:FindFirstChild(tag.Value) 
				local KillsData = DataStore2("kills", killer)
				if tag and killer then

					-- Leaderstats may not exist, so we'll find this properly too.
					local leaderstats = killer:WaitForChild("leaderstats", 2)
					if not leaderstats then
						error("Failed to find leaderstats folder in the killer.")
						return
					end

				--[[ From this point, we should be certain that the values in leaderstats will
				exist, but we'll use :FindFirstChild() anyway. ]]
					local killsValueObj = leaderstats:FindFirstChild("Kills")
					if not killsValueObj then
						error("Failed to find the 'Kills' object in the leaderstats folder.")
						return
					end
					KillsData:Increment(1)

					-- Same with the round kills.
					local roundKillsObj = killer:FindFirstChild("RoundKills")
					if not roundKillsObj then
						error("Failed to find the 'RoundKills' object in the killer.")
						return
					end
					roundKillsObj.Value += 1
				end
			end)
		end
	end)
	
end)

MareketPlaceService.ProcessReceipt = function(recipt)
	local player = Players:GetPlayerByUserId(recipt.PlayerId)
	-- -- -- -- -- --
	local pointsDataStore = DataStore2("points", player)
	local rubysDataStore = DataStore2("rubys", player)
	
	-- Points Products
	
	if recipt.ProductId == Products_Table_Points.ID1 then
		pointsDataStore:Increment(Prize_Table_Points.Prize1)
		print(player.Name.. " just bought the product: "..Products_Table_Points.ID1)
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	elseif recipt.ProductId == Products_Table_Points.ID2 then
		pointsDataStore:Increment(Prize_Table_Points.Prize2)
		print(player.Name.. " just bought the product: "..Products_Table_Points.ID2)
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	elseif recipt.ProductId == Products_Table_Points.ID3 then
		pointsDataStore:Increment(Prize_Table_Points.Prize3)
		print(player.Name.. " just bought the product: "..Products_Table_Points.ID3)
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	elseif recipt.ProductId == Products_Table_Points.ID4 then
		pointsDataStore:Increment(Prize_Table_Points.Prize4)
		print(player.Name.. " just bought the product: "..Products_Table_Points.ID4)
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	elseif recipt.ProductId == Products_Table_Points.ID5 then
		pointsDataStore:Increment(Prize_Table_Points.Prize5)
		print(player.Name.. " just bought the product: "..Products_Table_Points.ID5)
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	elseif recipt.ProductId == Products_Table_Points.ID6 then
		pointsDataStore:Increment(Prize_Table_Points.Prize6)
		print(player.Name.. " just bought the product: "..Products_Table_Points.ID6)
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
		-- Rubies
	elseif recipt.ProductId == Products_Table_Rubys.ID1 then
		rubysDataStore:Increment(Prize_Table_Rubys.Prize1)
		print(player.Name.. " just bought the product: "..Products_Table_Rubys.ID1)
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	elseif recipt.ProductId == Products_Table_Rubys.ID2 then
		rubysDataStore:Increment(Prize_Table_Rubys.Prize2)
		print(player.Name.. " just bought the product: "..Products_Table_Rubys.ID2)
		return Enum.ProductPurchaseDecision.PurchaseGranted

	elseif recipt.ProductId == Products_Table_Rubys.ID3 then
		rubysDataStore:Increment(Prize_Table_Rubys.Prize3)
		print(player.Name.. " just bought the product: "..Products_Table_Rubys.ID3)
		return Enum.ProductPurchaseDecision.PurchaseGranted

	elseif recipt.ProductId == Products_Table_Rubys.ID4 then
		rubysDataStore:Increment(Prize_Table_Rubys.Prize4)
		print(player.Name.. " just bought the product: "..Products_Table_Rubys.ID4)
		return Enum.ProductPurchaseDecision.PurchaseGranted

	elseif recipt.ProductId == Products_Table_Rubys.ID5 then
		rubysDataStore:Increment(Prize_Table_Rubys.Prize5)
		print(player.Name.. " just bought the product: "..Products_Table_Rubys.ID5)
		return Enum.ProductPurchaseDecision.PurchaseGranted

	elseif recipt.ProductId == Products_Table_Rubys.ID6 then
		rubysDataStore:Increment(Prize_Table_Rubys.Prize6)
		print(player.Name.. " just bought the product: "..Products_Table_Rubys.ID6)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

Thanks for the help!

Sincerely,

papahetfan