I’m trying to make a script that saves the players data. The script works by accessing the IntValues in the player’s leaderstats folder. Then takes the values stored in the IntValues and stores it inside a table named generalData. The table is then saved using SetAsync.
For some reason setAsync keeps failing to save. It keeps giving the error message.
Players.PlayerRemoving:Connect(function(player)
print("player left")
local ID = "GeneralSave:"..player.UserId
local generalData = {}
for i, stat in ipairs(player.leaderstats:GetChildren())do
table.insert(generalData, stat.Value)
end
local success, response = pcall(function()
generalDataStore:SetAsync(ID, generalData)
end)
if not success then
warn(response)
end
end)
The output keeps resulting in:
Update:
I changed the output of the error message I have displayed the new output.
Update 2:
OMG I JUST FREAKIN REALIZED THAT I MADE ROOKIE MISTAKE OF
local generalDataStore = DataStoreService:GetDecendants("GeneralDataStore")
INSTEAD OF
local generalDataStore = DataStoreService:GetDataStore("GeneralDataStore")
I would like to thank Forummer for bringing that part of the script to my attention or else I probably would not have figured this out until 10 years later or something.
I would also like to thank those who replied because yall provided valuable information to make this script more efficient.
Plus you haven’t added the player inside the playerremoving parameters, that will grab what player is being removed and then will be able to save data by that
Players.PlayerRemoving:Connect(function(p)
print("player left")
local ID = "GeneralSave:"..p.UserId
local generalData = {}
for i, stat in ipairs(p.leaderstats:GetChildren())do
table.insert(generalData, stat.Value)
end
local success, error = pcall(function()
generalDataStore:SetAsync(ID, generalData)
end)
if not success then
warn("Error")
end
end)
I’m not sure why you arent using the set function of a pcall function, when printing your error do.
Players.PlayerRemoving:Connect(function(p)
print("player left")
local ID = "GeneralSave:"..p.UserId
local generalData = {}
for i, stat in ipairs(p.leaderstats:GetChildren())do
table.insert(generalData, stat.Value)
end
local success, response = pcall(function()
generalDataStore:SetAsync(ID, generalData)
end)
if not success then
warn("Error, response: " .. response)
end
end)
The 2nd param of success, response/error, gives you the error of what you are facing.
So firstly, when each player is being removed it’ll override the previous table, therefore you’ll need to keep adding new arrays for each player which is stupid, I suggest calling a local function so that it’ll save each players data without overriding the other.
You don’t need to do tostring, just do warn(response) it’ll print the error and what line it’s erroring at which will be clickable and faster to see where it is.
After the player leaves, all of the instances including leaderstats are removed. Have you tried saving the leaderstats by referring to an array just in case the player leaves? If you saved the leaderstats in an array before the player leaves, you could save the data with the array.
Here’s an example:
local savedLeaderstats = {}
Players.PlayerAddedConnect(function(player)
print("player joined")
local leaderStatData = {}
savedLeaderstats[player.UserId] = leaderStatData
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local playerStats = {"Stage"} -- You could change the table to something you like. It may not be "Stage" you want to add.
for i, stat in pairs(playerStats) do
local statInst = Instance.new("IntValue", leaderstats)
statInst.Name = stat
statIns.Changed:Connect(function()
leaderStatData[stat] = statIns.Value
end)
leaderStatData[stat] = statIns.Value
end
end)
Players.PlayerRemoving:Connect(function(player)
print("player left")
local ID = "GeneralSave:"..player.UserId
local generalData = {}
for stat, value in pairs(savedLeaderstats[player.UserId])do
generalData[stat] = value
end
savedLeaderstats[player.UserId] = nil
local success, error = pcall(function()
generalDataStore:SetAsync(ID, generalData)
end)
if not success then
warn("Error")
end
end)