Probelm with Datastore

Hey, so the problem is that the “top” table only gives me the key and one value but I also need the second value because I have 2 Values in my Original Datastore table (Kills and RobuxDonated). So the thing is that for this it only gives me the Kills Value:

local smallestFirst = false
	local numberToShow = 100
	local minValue = 1
	local maxValue = 10e30
	local pages = database:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
	
	--Get data
	local top = pages:GetCurrentPage()--Get the first page
	print(top)

--TOP PRINTS JUST THE TABLE WITH THE KEY AND THE ROBUX DONATED VALUE, NOT THE KILLS VALUE

	local data = {}
	for _,v in ipairs(top) do--Loop through data
		local userid = v.key--User id
		local Kills = v.value--Kills

--I ALSO NEED THE ROBUX DONATED VALUE HERE

		local username = "[Failed To Load]"--If it fails, we let them know
		local s,e = pcall(function()
			username = game.Players:GetNameFromUserIdAsync(userid)--Get username
		end)
		if not s then--Something went wrong
			--warn("Error getting name for "..userid..". Error: "..e)
		end
		local image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		--Make a image of them
		if username ~= "[Failed To Load]" then
			table.insert(data,{username,Kills,image,userid})--Put new data in new table
		end
			
	end

This is the full code:

--//Services
local DataStoreService = game:GetService("DataStoreService")
local DataStoreService = game:GetService("DataStoreService")
local RS = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local MarketplaceService = game:GetService("MarketplaceService")
local SSS = game:GetService("ServerScriptService")
local Players = game:GetService("Players")

--//Required modules
local PlayerDataManager = require(game:GetService("ServerScriptService").Data.PlayerData.Modules.DataManager)

--//Variables
local SurfaceGui = script.Parent.DisplayPart.SurfaceGui
local SurfaceGui2 = script.Parent.Parent.LeaderBoard2.DisplayPart.SurfaceGui
local sample = script:WaitForChild("Sample")
local sample2 = script:WaitForChild("Sample")
local ScrollingFrame = SurfaceGui:WaitForChild("ScrollingFrame")
local ScrollingFrame2 = SurfaceGui2:WaitForChild("ScrollingFrame")

local database = DataStoreService:GetOrderedDataStore("Leaderboard4")

--//Functions
local function formatNumberWithCommas(number)
	local formattedNumber = tostring(number)
	formattedNumber = formattedNumber:reverse():gsub("(%d%d%d)", "%1,")
	return formattedNumber:reverse():gsub("^,", "")
end

function UpdateLeaderBoard()
	local smallestFirst = false
	local numberToShow = 100
	local minValue = 1
	local maxValue = 10e30
	local pages = database:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
	
	--Get data
	local top = pages:GetCurrentPage()--Get the first page
	print(top)
	local data = {}
	for _,v in ipairs(top) do--Loop through data
		local userid = v.key--User id
		local money = v.value--Money
		local username = "[Failed To Load]"--If it fails, we let them know
		local s,e = pcall(function()
			username = game.Players:GetNameFromUserIdAsync(userid)--Get username
		end)
		if not s then--Something went wrong
			--warn("Error getting name for "..userid..". Error: "..e)
		end
		local image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		--Make a image of them
		if username ~= "[Failed To Load]" then
			table.insert(data,{username,money,image,userid})--Put new data in new table
		end
			
	end
	for i,v in pairs(ScrollingFrame:GetChildren()) do--Remove old frames
		if v:IsA("Frame") then
			v:Destroy()
		end
	end

	for position,dataValues in pairs(data) do--Loop through our new data
		local name = dataValues[1]
		local val = dataValues[2]
		local val2 = dataValues[2]
		local image = dataValues[3]
		local userid = dataValues[4]
		local color = Color3.new(1,1,1)--Default color
		
		if position == 1 then
			color = Color3.new(1,1,0)--1st place color
			
			local RigTemplate = script.Parent.Podium.R6
			
			RigTemplate:ScaleTo(1)
			
			RigTemplate.HumanoidRootPart.CFrame = RigTemplate.HumanoidRootPart.CFrame * CFrame.new(0,50,0)
					
			local PlayerDescription = game.Players:GetHumanoidDescriptionFromUserId(userid)--Get player apparence
			RigTemplate.Humanoid:ApplyDescription(PlayerDescription) --Load player apparence to rig
			RigTemplate.Humanoid:LoadAnimation(script.Animation):Play() -- Rig dance
			
	
			task.delay(1,function()
				RigTemplate.HumanoidRootPart.CFrame = RigTemplate.HumanoidRootPart.CFrame * CFrame.new(0,-50,0)
				RigTemplate:ScaleTo(2)
			end)
			
		elseif position == 2 then
			color = Color3.new(0.619608, 0.619608, 0.619608)--2nd place color
		elseif position == 3 then
			color = Color3.fromRGB(166, 112, 0)--3rd place color
		end
		
		local new = sample:Clone()--Make a clone of the sample frame
		new.LayoutOrder = position--UIListLayout uses this to sort in the correct order
		new.Image.Image = image--Set the image
		new.Image.Place.Text = string.format('#%s',position)--Set the place
		new.Image.Place.TextColor3 = color--Set the place color (Gold = 1st)
		new.PName.Text = name--Set the username
		new.Value.Text = formatNumberWithCommas(val)--Set the amount of money
		new.PName.TextColor3 = color--Set the place color (Gold = 1st)
		new.Parent = ScrollingFrame--Parent to scrolling frame
		task.wait(.1)
	end
end

game.Players.PlayerRemoving:Connect(function(plr)
	task.delay(.5, function()
		pcall(function()
			-- Update kills value
			database:UpdateAsync(plr.UserId, function(oldVal)
				return tonumber(plr:WaitForChild('leaderstats'):FindFirstChild("Total Kills").Value)
			end)

			-- Update Robux donated value
			database:UpdateAsync(plr.UserId, function(oldVal)
				return tonumber(plr:WaitForChild('leaderstats2'):FindFirstChild("RobuxDonated").Value)
			end)

		end)
		warn('Leaderboard Updated!')
		UpdateLeaderBoard()
	end)
end)

task.spawn(function()
	while task.wait(60) do
		UpdateLeaderBoard()
	end
end)

local Players = game:GetService("Players")

local sessionData = {}

local function SaveGame(plr)
	if sessionData[plr.UserId] then

		local succses = nil
		local errorMsg = nil
		local attempt = 1

		repeat
			succses, errorMsg = pcall(function()	
				database:SetAsync(plr.UserId, sessionData[plr.UserId])
			end)

			attempt += 1
			if not succses then
				warn(errorMsg)
				task.wait(3)
			end
		until succses or attempt == 5 --tries until is succses or until it tried 5 times

	end
end


function PlayerAdded(plr)

	local Kills = plr:WaitForChild("leaderstats"):WaitForChild("Total Kills"):Clone()
	local RobuxDonatedValue = plr:WaitForChild("leaderstats2"):WaitForChild("RobuxDonated")

	local succses = nil
	local plrData = nil
	local attempt = 1

	repeat
		succses, plrData = pcall(function()	
			return database:GetAsync(plr.UserId)
		end)

		attempt += 1
		if not succses then
			warn(plrData)
			task.wait(3)
		end
	until succses or attempt == 5 --tries until is succses or until it tried 5 times
	if succses then
		print("Connected to database", plr.Name)
		if not plrData then
			print("Assigning default data", plr.Name)
			plrData = {
				["Kills"] = 0,
				["RobuxDonated"] = 0,
			}
		end	
		sessionData[plr.UserId] = plrData
	else
		warn("Unable to get data for", plr.UserId)
		plr:Kick("Unable to load data. We are sorry,")
	end
	
	plrData = {
		["Kills"] = 0,
		["RobuxDonated"] = 0,
	}

	plrData.Kills = Kills.Value
	Kills.Changed:Connect(function()
		plrData.Kills = Kills.Value
	end)
	
	plrData.RobuxDonated = RobuxDonatedValue.Value
	RobuxDonatedValue.Changed:Connect(function()
		plrData.RobuxDonated = RobuxDonatedValue.Value
	end)
	
	print(plrData)
	
	UpdateLeaderBoard()

end
Players.PlayerAdded:Connect(PlayerAdded)


function PlayerLeaving(plr)
	SaveGame(plr)
end
Players.PlayerRemoving:Connect(PlayerLeaving)

Tell me if you need more info and thanks in advance.

1 Like

just bumping this again (qgdgqvqdqd)

1 Like

Yeah if you need the key and the value
Use the explanation below
Local dic = {} --write your data here (probably the data you need to save)
For index, item in pairs(dic) do

End
The index would be the key
The item would be the value of the key