Hi guys I have written a small leaderboard saving here which should normally store the data and output it again. Problem is that it does not do this and does not give an error message. It would be a dream if you could help me with this problem.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("Datalore")
Players.PlayerAdded:Connect(function(player)
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(tostring(player.UserId))
end)
if success then
if Data then
for i, v in pairs(Data) do
player:WaitForChild("leaderstats"):WaitForChild(i).Value = v
end
end
else
error(errormessage)
end
end)
local function Save(player)
local SavedData = {}
for _, v in pairs(player.leaderstats:GetChildren()) do
SavedData[v.Name] = v.Value
end
local success, errormessage = pcall(function()
Saver:SetAsync(tostring(player.UserId), SavedData)
end)
if not success then
error(errormessage)
end
end
Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
Save(v)
end
end)
Unless you are doing so in another script, you aren’t actually creating the leaderstats for the player. Roblox will not save the leaderstats folder when the player leaves, so you need to manually create the instances each time they join.
DataStore Access: Make sure you have DataStore access enabled in your game’s settings.
Leaderstats Initialization: Make sure your leaderstats are initialized for each player before trying to access them.
if not working then Try this code:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetDataStore("Datalore")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Initialize leaderstats with default values if necessary
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
local Data = nil
local success, errormessage = pcall(function()
Data = Saver:GetAsync(tostring(player.UserId))
end)
if success then
if Data then
for i, v in pairs(Data) do
player.leaderstats:WaitForChild(i).Value = v
end
end
else
warn("Error loading data for player " .. player.Name .. ": " .. errormessage)
end
end)
local function Save(player)
local SavedData = {}
for _, v in pairs(player.leaderstats:GetChildren()) do
if v:IsA("IntValue") or v:IsA("NumberValue") then
SavedData[v.Name] = v.Value
end
end
local success, errormessage = pcall(function()
Saver:SetAsync(tostring(player.UserId), SavedData)
end)
if not success then
warn("Error saving data for player " .. player.Name .. ": " .. errormessage)
end
end
Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
Save(v)
end
end)
Unfortunately, this code does not work for me either.
But at the end, when I close the game, there is an error code → DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key =