Hello,
In my class-based game, I’ve been trying to make a datastore that saves stats like Kills and Wins for every individual class in the game. For some reason, it doesn’t seem to want to work. I’m not sure why it doesn’t want to save nor do I know how to fix it.
My guesses as to why it’s not working are:
saving by putting values in a table via table.insert makes it not save
trying to save values inside a folder inside a folder makes it not save
It’s really confusing, but I know there has to be an efficient way of saving individual class stats. I’ve seen it in a few other games.
local DS = game:GetService("DataStoreService"):GetDataStore("PlayerStats")
game.Players.PlayerAdded:Connect(function(p)
local fo = Instance.new("Folder",p)
fo.Name = "ClassStats"
for i, v in pairs(game.ServerStorage.Classes:GetChildren()) do
local f = Instance.new("Folder", fo)
f.Name = v.Name
local w = Instance.new("IntValue",f)
w.Name = "Wins"
local k = Instance.new("IntValue",f)
k.Name = "Kills"
end
wait(1)
local plrKey = "stats_"..p.UserId
local GetSaved = DS:GetAsync(plrKey)
if GetSaved then
for i, v in pairs (p.ClassStats:GetChildren()) do
v.Wins.Value = GetSaved[#GetSaved + 1]
v.Kills.Value = GetSaved[#GetSaved + 1]
end
else
local NumbersForSaving = {}
for i, v in pairs (p.ClassStats:GetChildren()) do
table.insert(NumbersForSaving, v.Wins.Value)
table.insert(NumbersForSaving, v.Kills.Value)
end
DS:GetAsync(plrKey, NumbersForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(p)
local Table = {}
for i, v in pairs (p.ClassStats:GetChildren()) do
table.insert(Table, v.Wins.Value) ----- I'm certain it has to do with this
table.insert(Table, v.Kills.Value)
end
DS:SetAsync("stats_"..p.UserId, Table)
end)
for i, v in pairs (p.ClassStats:GetChildren()) do
table.insert(Table, #Table + 1, v.Wins.Value)
table.insert(Table, #Table + 1, v.Kills.Value)
end
That didn’t seem to make it save… Could it have to do with trying to save values inside of a folder inside of a folder? Or is it trying to put values in a table using table.insert making it not save?
local data
local success, err = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
if success then
value.Value = data
value.Value = data
end
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
DataStore:SetAsync(player.UserId,{player.Folder.value.Value,player.Folder.value.Value})
end)
if success then
warn("Saved Data")
else
warn(err)
end
end)
Yeah now I figured out it’s not a saving problem but a loading problem, as I had just made a small block of code telling the script to list every value being saved, and it showed the changed values. When they load in, they aren’t the saved values they’re supposed to be
If you are adding a number value to a table, you must do table.insert(table,#table+1,your_value) Second parameter is for the place it will have. Let’s say you’re adding an object to a table: table.insert(table,object), that will be fine however it’s not a number value & it doesn’t save it to a specific place. However, if you were to do this:
local table = {"Roblox";"Is";"Best"}
table.insert(table,3,"The") --insert "The" to the 3rd place
for _,v in pairs(table) do --just a loop through table
print(table[3]) --get the 3rd thing in the table
end
You would get: “Roblox Is The Best” printed.
In your case, you would need to do:
game.Players.PlayerRemoving:Connect(function(p)
local Table = {}
for i, v in pairs (p.ClassStats:GetChildren()) do
table.insert(Table,#Table+1,v.Wins.Value)
table.insert(Table,#Table+1,v.Kills.Value)
end
DS:SetAsync("stats_"..p.UserId, Table)
end)
Turns out, the problem was that I was trying to add to a table that was already completed. This table already had saved values, but I was trying to add onto it rather than to find these values.
I used a method similar to what you posted here and it ended up working:
local GetSaved = DS:GetAsync(plrKey)
if GetSaved then
local num = 0
for i, v in pairs (p.ClassStats:GetChildren()) do
num = num + 1
v.Wins.Value = GetSaved[num]
num = num + 1
v.Kills.Value = GetSaved[num]
end
else ----- the code below is irrelevant
local NumbersForSaving = {}
for i, v in pairs (p.ClassStats:GetChildren()) do
table.insert(NumbersForSaving, #NumbersForSaving + 1, v.Wins.Value)
table.insert(NumbersForSaving, #NumbersForSaving + 1, v.Kills.Value)
end
wait()
DS:GetAsync(plrKey, NumbersForSaving)
end
The second argument isn’t necessary, Lua automatically adds new items to the end of a table.
Edit: Nevermind, when I tested this I was using strings. The second argument is needed
BTW, Op, make sure to use pcalls when saving and loading data.