Table.insert makes values unable to be saved in a datastore for some reason

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)

you are using table.insert wrong.

try changing to :

table.insert(Tables,#Tables +1,v.Wins.Value) -- second parameter is the index of the value in the 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

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?

are there any errors or do they just not save?

It doesn’t show any errors in the output. It just doesn’t save

Try playing the game using the roblox client and not in studio.

Use pcall like this:

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)
1 Like

Make sure it is also serverscript

You also need API on from game settings > security > API Service

yeah it’s both a server script and API is on

did you try playing it using the Roblox Player?

yeah it doesn’t even work outside of studio

Same thing as inside of studio: it doesn’t save for some reason

I tried this out, and for some reason, it doesn’t print success nor error

Actually, forget what I said. It does print “Saved Data”, however it doesn’t actually save the Kills or Wins

Then there might be a problem loading data.

for i, v in pairs (p.ClassStats:GetChildren()) do
v.Wins.Value = GetSaved[#GetSaved + 1]
v.Kills.Value = GetSaved[#GetSaved + 1]
end

I think this is wrong. It looks like you’re getting the same index from the table for both values.

for i, v in pairs (p.ClassStats:GetChildren()) do
v.Wins.Value = GetSaved[1]
v.Kills.Value = GetSaved[2]
end

Try this maybe? ^

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 :woman_facepalming:

BTW, Op, make sure to use pcalls when saving and loading data.

1 Like