Problem with Data stores

Hi, I’m a novice developer creating a storygame. I ran across a problem with leaderstats saving (Surprisingly it saves on the leaderboard) but not the leaderstats.

Leaderstats code:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Wins") 

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "Leaderstats"
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"	
	wins.Parent = leaderstats
	local data
	local suc, err = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-wins")
	end)
	
	if suc then
		wins.Value = data
	else
		warn(err)
		print("error getting data")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local suc,err = pcall(function()
		DataStore:SetAsync(player.UserId.."-wins", player.Leaderstats.Wins.Value)
	end)
	if suc then
		print("Data successfully saved!")
	else
		print("there was an error saving data")
		warn(err)
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local billboard = Instance.new("BillboardGui")
	local textlabel = Instance.new("TextLabel")
	local hrp = plr.Character:WaitForChild("HumanoidRootPart")

	billboard.Parent	= plr.Character
	billboard.Adornee	= hrp
	billboard.Active = true
	billboard.MaxDistance = 1000
	billboard.ResetOnSpawn = false
	billboard.StudsOffset = Vector3.new(0, 4, 0)
	billboard.Size = UDim2.new(0, 200 , 0, 50)
	billboard.Name = "FighterGui"

	textlabel.Parent = billboard
	textlabel.BackgroundTransparency = 1
	textlabel.Visible = true
	textlabel.TextScaled = true
	textlabel.Size = UDim2.new(0, 200 , 0, 50)
	textlabel.Name = "LabelFighter"
	textlabel.Text = "Traveler"
	textlabel.Font = Enum.Font.Ubuntu
	textlabel.TextColor3 = Color3.new(0.1, 0.8, 0.38)
		textlabel.RichText = true
		if plr.Leaderstats.Wins.Value >= 5 then
			textlabel.Text = "Explorer"
		textlabel.TextColor3 = Color3.new(0.38, 0.8, 0.8)end 
	if plr.Leaderstats.Wins.Value >= 15 then
		textlabel.Text = "Tourist"
		textlabel.TextColor3 = Color3.new(0.1, 0.1, 0.8)end 
	if plr.Leaderstats.Wins.Value >= 25 then
		textlabel.Text = "Tour Guide"
		textlabel.TextColor3 = Color3.new(0.7, 0, 0.7)end 
	if plr.Leaderstats.Wins.Value >= 35 then
		textlabel.Text = "Flight Attendant"
		textlabel.TextColor3 = Color3.new(0.3, 0, 0.3)end 
	if plr.Leaderstats.Wins.Value >= 50 then
		textlabel.Text = "Expert Explorer"
		textlabel.TextColor3 = Color3.new(0.9, 0.86, 0)end 
	if plr.Leaderstats.Wins.Value >= 65 then
		textlabel.Text = "Business Traveler"
		textlabel.TextColor3 = Color3.new(1, 0.66, 0)end	
end)

Leaderboard code:

local Players=game:GetService('Players')
local TempPart=script.Parent
--[[]]--
local SurfaceGui=TempPart.SurfaceGui
local MainFrame=SurfaceGui.Frame
--[[]]--
local Template=script['Template']
--[[]]--
local RecentlySaved={}
local DataStoreService=game:GetService('DataStoreService')
local GlobalLeaderboardB=DataStoreService:GetOrderedDataStore(":Wins")
if not game["Run Service"]:IsStudio() then
	GlobalLeaderboardB=DataStoreService:GetOrderedDataStore("Game:Wins")
end

local CurrencyName="Wins"
local ListSize=10
local UpdateEvery=5
local MinimumRequirement=1
--[[]]--
local _functions={}
_functions.returncurrency = function(v)
	local x=0
	for a, b in pairs(v:GetDescendants()) do
		if b.Name==CurrencyName then
			x=b.Value
			break
		end
	end
	return x
end
_functions.removefromrecentlysaved=function(PlayerName)
	for i, v in pairs(RecentlySaved) do 
		if v==PlayerName then
			table.remove(RecentlySaved,i)
			break
		end
	end
end
_functions.autoremoverecentsaved=function(PlayerName)
	spawn(function()
		wait(15)
		for i, v in pairs(RecentlySaved) do 
			if v==PlayerName then
				table.remove(RecentlySaved,i)
				break
			end
		end
	end)
end
_functions.returnplayerlist=function()
	local int = 0
	for i, v in pairs(Players:GetPlayers()) do 
		for _, j in pairs(v:GetDescendants()) do 
			if j.Name==CurrencyName and j.Value>MinimumRequirement then
				int+=1
				break
			end
		end
	end
	return int
end
_functions.clear=function()
	for i, v in pairs(MainFrame:GetChildren()) do 
		if v:IsA('Frame') then
			v:Destroy()
		end
	end
end
_functions.abbreviate=function(value,idp)
	if value < 1000 then
		return math.floor(value + 0.5)
	else
		local abbreviations = {"", "K", "M", "B", "T"}
		local ex = math.floor(math.log(math.max(1, math.abs(value)),1000))
		--[[]]--
		local abbrevs = abbreviations [1 + ex] or ("e+"..ex)
		local normal = math.floor(value * ((10 ^ idp) / (1000 ^ ex))) / (10 ^ idp)
		--[[]]--
		return ("%."..idp.."f%s"):format(normal, abbrevs)
	end
end
--[[]]--
Players.PlayerRemoving:Connect(function(Player)
	local PName=Player.Name
	if not table.find(RecentlySaved,Player.Name) then
		table.insert(RecentlySaved,Player.Name)
		local CurrentCurrencyAmount=_functions.returncurrency(Player)
		local Success,Errormsg=pcall(function()
			GlobalLeaderboardB:SetAsync(Player.UserId,CurrentCurrencyAmount)
		end)
		if not Success then warn(Errormsg) end
		_functions.removefromrecentlysaved(PName)
	else 
	end
end)
--[[]]--
while true do 
	_functions.clear()
	repeat wait() until _functions.returnplayerlist()>0
	for i, Player in pairs(Players:GetPlayers()) do 
		local PName=Player.Name
		if not table.find(RecentlySaved,Player.Name) then
			table.insert(RecentlySaved,Player.Name)
			local CurrentCurrencyAmount=_functions.returncurrency(Player)
			local Success,Errormsg=pcall(function()
				GlobalLeaderboardB:SetAsync(Player.UserId,CurrentCurrencyAmount)
			end)
			if not Success then warn(Errormsg) end
			_functions.autoremoverecentsaved(PName)
		end
	end
	local Pages = GlobalLeaderboardB:GetSortedAsync(false, ListSize)
	local TopList = Pages:GetCurrentPage()
	for Rank, SavedData in ipairs(TopList) do
		local UserId = SavedData.key
		local CAmount=SavedData.value
		--[[]]--
		if CAmount>=MinimumRequirement then
			local Template=script['Template']:Clone()
			Template.Parent=MainFrame
			Template.LayoutOrder=Rank
			Template.Score.Text="".._functions.abbreviate(CAmount,1)
			local function SetRankText(n)
				local A={}
				A[1]=function()
					Template.Rank.Text="1"
				end
				A[2]=function()
					Template.Rank.Text="2"
				end
				A[3]=function()
					Template.Rank.Text="3"
				end
				if n<=#A then
					A[n]()
				else Template.Rank.Text="#"..n
				end
			end
			SetRankText(Rank)
			--[[]]--
			local User="Unknown"
			local Success,Errormsg=pcall(function()
				User=Players:GetNameFromUserIdAsync(UserId)
			end)
			Template.Username.Text=User
			Template.Name=User
		end
	end
	task.wait(UpdateEvery)
end

Also there were no outputs in the pcall functions, and surprisingly no errors or warnings.

1 Like

Where is Player Removing part on leaderstats?

1 Like

Wdym player removing? β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž

β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž

1 Like

Ok i just skipped it accidentaly lol

You should put the pcalls inside a repeat until loop. Because SetAsync/GetAsync doesn’t like to work sometimes

local attempt = 1

repeat
-- ur code of saving and getting the data
if not suc then
warn(err)
task.wait(3)
end
attempt += 1
until suc or attempt == 5
1 Like