105: Serialized value exceeds 4MB limit

Hello, I need help to save table with DataStore.

Here is my code:

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("BusinessDatastore")

local currency = {
	Name = {""};
	Description = {""};
	Color = {""}
}

game.Players.PlayerAdded:Connect(function(plr)
	
	currency.Name = ds:GetAsync(plr.UserId) or ""
	currency.Description = ds:GetAsync(plr.UserId) or ""
	currency.Color = ds:GetAsync(plr.UserId) or "0, 0, 0"

	game.ReplicatedStorage.SaveMoneyEvent.OnServerEvent:Connect(function()
		ds:SetAsync(plr.UserId, currency)
	end)

	while wait(10) do
		ds:SetAsync(plr.UserId, currency)

		currency.Name = script.Parent.NameValue.Value
		currency.Description = script.Parent.Description.Value
		currency.Color = script.Parent.Color.Value
		
		print(currency)
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync(plr.UserId, currency) 
end)

Thanks you if you help me!

Are you talking about the error in the title? If so, that means you need to free up space and clean your data store.

This topic may help: Serialized value exceeds 4MB limit

Yes but, I have another error:

104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. - Serveur - Save:21

It didn’t help me, sorry.
Do you have another thing to help me?

There are multiple issues with this code:

  • Use should use :GetService whenever referencing a service, so it should be game:GetService("Players").
  • All of the {""} should be "", and Color = {""} should be Color = {}, and currency should likely be defined inside the .PlayerAdded function.
  • You are doing 3 separate :GetAsync calls, which are HTTP requests and have rate limits and are not very fast. You should instead do a :GetAsync call once and store it’s result in the table. Also, because you are saving a dictionary ds:GetAsync(plr.UserId) with return a dictionary, and so it will error when you try and set current.Name or the others using it.
  • Generally, there is no point in doing this, but you shouldn’t use wait (use task.wait instead) and the task.wait should be inside the loop, with the top being while true do instead because while wait() do is not how while loops are meant to be used.
  • You should wrap all :GetAsync and :SetAsync in pcalls because they can error, like so:
local Success, Info = pcall(function()
	return ds:GetAsync(plr.UserId)
end)

if not Success then
	warn("An error occurred! Error: " .. Info)
else
	currency = Info
end

Here is the script with some of this implemented:

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("BusinessDatastore")
local Players = game:GetService("Players")

local playerCurrencies = {}

Players.PlayerAdded:Connect(function(plr)
	playerCurrencies[plr] = ds:GetAsync(plr.UserId)
	
	if not playerCurrencies[plr] then
		playerCurrencies[plr] = {
			Name = "DefaultName",
			Description = "DefaultDescription",
			Color = {0, 0, 0}
		}
	end
	
	currency = playerCurrencies[plr]
	
	game:GetService("ReplicatedStorage").SaveMoneyEvent.OnServerEvent:Connect(function()
		ds:SetAsync(plr.UserId, currency)
	end)

	while true do
		ds:SetAsync(plr.UserId, currency)

		currency.Name = script.Parent.NameValue.Value
		currency.Description = script.Parent.Description.Value
		currency.Color = script.Parent.Color.Value
		
		print(currency)
		task.wait(10)
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync(plr.UserId, currency) 
end)
1 Like

Thanks for your time!
I try your script, I have the same error:

You need to json encode the data.

Can you show a picture of the data you are trying to save? I think it may contain illegal characters (non UTF-8) which the datastore cannot save without extra steps.

1 Like