DataStore Array Script Suddenly Stopped Working!

Hello! I experienced this Error today. I wrote this script earlier and it worked great! Out of no where, it stopped working and would no longer save data. I know It will not Save when the player object is removed due to the fact the the Data Table is not getting encoded due to some unknown error while encoding the Data Table. My output is fully clear and I tried reverting script versions, made sure the API is Enabled and attempted troubleshooting. I pasted my code below. Keep in mind that it was working just fine earlier when I wrote it . Strange :thinking:

Blockquote
local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“DataStore523”)
local Players = game:GetService(“Players”)
local HttpService = game:GetService(“HttpService”)
local Table = {}
Players.PlayerAdded:Connect(function(Player)
local Key = “Key-”..Player.UserId
local Folder = Instance.new(“Folder”)
Folder.Name = “leaderstats”
Folder.Parent = Player

local Value1 = Instance.new("NumberValue")
Value1.Name = "Money"
Value1.Parent = Folder

local Value2 = Instance.new("IntValue")
Value2.Name = "Cash"
Value2.Parent = Folder

local Data = DataStore:GetAsync(Key)

if Data then
	Table = Data or {}
	for i,v in pairs(Table) do
		Value1.Value = v[1]
		Value2.Value = v[2]
	end
else
	Value1.Value = 500
	Value2.Value = 0
end

end)

Players.PlayerRemoving:Connect(function(Player)
local Key = “Key-”..Player.UserId
local Folder = Player.leaderstats
table.clear(Table)
table.insert(Table, {Folder.Money.Value, Folder.Cash.Value})
DataStore:SetAsync(Key, Table)
print(HttpService:JSONEncode(Table))
end)

but on the PlayerRemoving you didnt encode the data you just save the table as him, without encode it into JSON string
DataStoreService Cannot store tables only string
Before SetAsync Add

Table = HttpService:JSONEncode(Table)

And Change Table = Data or {} with:

Table = HttpService:JSONDecode(Data)

that will convert your data to string before store it and convert it back to a table after you get it

1 Like

Appreciate your help however, I once used HttpService to print the table. I previously created a script with many similarities to this one however, that one works. I believe I pinned down what may be causing it.

what is the output error? also is datastore API enabled?

You print the table Encoded but you didnt store it like that you store it as table
in this case the data will still nil

Did not get any despite it working earlier. Yes, Both HTTP and API services are obviously enabled lol.

are you testing this in studio or a live game? Studio’s play solo can sometimes shutdown before the server gets time tomsave the data. You can use:

game:BindToClose(function()
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
         -- save data
    end
end)