DataStore loading but not saving

So i am trying to get datastore to work and i have done it many times before and i am using the same setup for the saving and loading of the data the weird part is that all the times i’ve used my script so far i’ve never ever had any issues as in the title i have checked so many times and it simply rarely saves it seams to be like 1 out of 10 or so that works but the loading of the data works just fine
here is my script

local datastore = game:GetService("DataStoreService")
local key = 2
local MainStore = datastore:GetDataStore("Main")
local failed = {}
local datastoreOn = true
local eternity = require(game.ReplicatedStorage.EternityNum.EternityNum)

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder", player)
	stats.Name = "stats"
	
	-- stats --
	local statA = Instance.new("StringValue", stats)
	statA.Name = "statA"
	statA.Value = "0"
	
	local stat1 = Instance.new("StringValue", stats)
	stat1.Name = "stat1"
	stat1.Value = table.concat(eternity.convert(0), ",")
	
	local stat2 = Instance.new("StringValue", stats)
	stat2.Name = "stat2"
	stat2.Value = table.concat(eternity.convert(0), ",")
		
	if datastoreOn then
		local dataMain
		local succes, errorMessage = pcall(function()
			dataMain = MainStore:GetAsync(tostring(player.UserId).."Main"..tostring(key)) -- works!
		end)
		
		if dataMain then
			if tonumber(dataMain[1]) + 3 == #dataMain then
				statA.Value = dataMain[1]
				stat1.Value = dataMain[2]
				stat2.Value = dataMain[3]
				
				for i = 1, tonumber(statA.Value), 1 do
					local stat = Instance.new("StringValue", stats)
					stat.Name = "stat"..(i + 2)
					stat.Value = dataMain[i + 3]
				end
			end
		elseif not succes then
			failed[player.Name] = player.Name
			player:Kick("Your data was not recieved from server please rejoin")
		end
	end
	
	if not failed[player.Name] then
		for i = 1, tonumber(statA.Value) + 1, 1 do
			local section = game.ReplicatedStorage.Models.Section:Clone()
			section:SetPrimaryPartCFrame(CFrame.new(i * 20 - 20, 0.5, 0))
			section.Parent = workspace.Map
			section.Name = "section"..tostring(i)
			section.Button.Type.Value = tostring(i)
			section.Button.Button.BrickColor = BrickColor.Random()
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	if datastoreOn then
		if not (failed[player.Name]) then
			local main = {}
			main[1] = player.stats.statA.Value
			main[2] = player.stats.stat1.Value
			main[3] = player.stats.stat2.Value
			
			for i = 1, tonumber(player.stats.statA.Value), 1 do
				main[i + 3] = player.stats:FindFirstChild("stat"..(i + 2))
			end
			
			pcall(function()
				MainStore:SetAsync(tostring(player.UserId).."Main"..tostring(key), main) -- does not work!
			end)
		else
			failed[player.Name] = nil
		end
	end
end)

I’ve been debugging this for hours and the only way i can see that it is wrong is just roblox being drunk for the millioth time. No errors in console

Have you tried testing it in-game to see if it’s just Studio acting up?

I have tryed it in-game as i know studio is broken when it comes to datastore

Try adding print("success") after every if-then statement to see where it’s not working.

I’ve tryed print() everywhere including where i am saving it does return true for the success but it just isn’t saving as when i rejoin the table is the exact same length as the prevoius data had

Are you modifying the data on the client or server?

Like let’s say you join with 5 coins and want to change it to 10 coins, is the script that changes it a local or server script?

this is all done on the server

Do

print(main)

And check the output to see if the values are actually changing.

the data going into the saving part is correct so main is as it should be

Try removing the pcall function part.

still the same also can’t really see why it could be the problem as my other games use the same thing

It was sort of a last effort, I don’t know what the issue is.

maybe i should say how the data is so here it is
what is already stored

main = {
     [1] = "0",
     [2] = "0,0,0",
     [3] = "0,0,0"
}

example of what i want to store

main = {
     [1] = "0",
     [2] = "0,0,0",
     [3] = "0,0,0",
     [4] = "0,0,0"
}

what it is after the saving

main = {
     [1] = "0",
     [2] = "0,0,0",
     [3] = "0,0,0"
}

Try using table.insert() to add the extra Index.

still the same issues. i am really starting to think it is just roblox that is broken for some reason

So i’ve now tryed to change the SetAsync function out with the UpdateAsync function as i’ve read that it is recommended over the SetAsync function and it is still broken

Update on my issue it is still not working.

If you’re testing this in studio, the PlayerRemoving event might not fire everytime.
I also recommend saving on an interval, including a BindToClose event on top of that for extra security.

You should also set up something that can handle errors. Example:

local Succ, Err = pcall(function()
	return Key:SetAsync() -- example.
end)
if Succ then
	print('Data has been saved')
else
	warn('Data could not be saved!')
end

-- You should try to add a repeat loop in case it fails.

Useful link: Here.

So i just experimented with DataStore2 and it doesn’t have a problem but anyway thanks for trying to help