[ DataStore2 ] How to save and load table?

i don’t care how to load and save DataStore2, every time getting this error :

:arrow_forward: Not running script because past shutdown deadline (x34)

:arrow_forward:DataStoreService: CantStoreValue: Cannot store Dictionary in data store. Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters.

Here is my code :

local MarketPlaceService = game:GetService('MarketplaceService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TextChatService = game:GetService("TextChatService")
local BadgeService = game:GetService('BadgeService')
local RunService = game:GetService('RunService')
local Players = game:GetService('Players')

------------------------------------------------------------------

local AssetFolder = ReplicatedStorage.AssetFolder
local Modules = ReplicatedStorage.Modules
local SystemsModules = Modules.Systems
local Events = ReplicatedStorage.Events

local towerInfo = require(Modules.TowerInfo)

local getData = Events.getData

------------------------------------------------------------------

local DataStoreKey = 'U:TD_Main'

local DataStore2 = require(SystemsModules.DataStore2)
DataStore2.Combine(DataStoreKey, 'U:TD')

local Data = {}
local DataExample = {
	['Coins'] = 250,
	['Donate'] = 0,
	['Gems'] = 0,

	['GamesPlayed'] = 0,
	['Loses'] = 0,
	['Wins'] = 0,

	['loggedBefore'] = os.time,

	['OwnedTowers'] = {'Shooter'},
	['SelectedTowers'] = {'Shooter'},

	['OwnedSkins'] = {},
	['SelectedSkins'] = {},

	['OwnedPowerups'] = {},
	['SelectedPowerups'] = {},

	['OwnedEmotes'] = {},
	['EquippedEmotes'] = {},

	['CodesRedeemed'] = {},
	['Level'] = 1,

	['Battlepass'] = {},
	['BattlepassExp'] = 125,
	['BattlepassLevel'] = 100,
	["BattlepassExpNeed"] = 150,

	['MaxSelectedTowers'] = 5,
	['MaxSelectedPowers'] = 5
}

local Badges = {
	['Voider'] = 12345678, -- // Put Bage id here.
}

------------------------------------------------------------------

local function loadData(Player : Player)
	local Success = nil
	local playerData = nil
	local attempt = 1
	
	local Storage = DataStore2(DataStoreKey, Player)
	
	repeat 
		Success, playerData = pcall(function()
			return Storage:Get({})
		end)
		
		attempt += 1
		
		if not Success then
			task.wait(0.25)
		end
	until Success or attempt >= 3
	
	if Success then
		if playerData ~= nil then
			Data[Player.UserId] = playerData
		else
			Data[Player.UserId] = DataExample
			print('g')
		end
		
		for i, v in pairs(Badges) do
			if BadgeService:UserHasBadgeAsync(Player.UserId, v) then
				if not table.find(playerData.OwnedTowers, i) then
					table.insert(playerData.OwnedTowers, i)
				end
			end
		end		
	else
		return Player:Kick('DataStore2 error, please rejoin later.')
	end
end

local function SaveData(Player : Player)
	if Data[Player.UserId] then
		local Storage = DataStore2(DataStoreKey, Player)

		Storage:Set(Data[Player.UserId])
	end
end

local kickActivity = true

Players.PlayerAdded:Connect(function(Player)
	task.spawn(function()
		loadData(Player)
		_G['Data'] = Data
		
		for i, v in pairs(Admins) do
			if kickActivity then
				if not table.find(Admins, v) then
					Player:Kick('\nGame is closed right now, we are sorry. \n\n Come back later!')
				end
			end
		end
	end)
end)

Players.PlayerRemoving:Connect(function(Player)
	SaveData(Player)
end)

------------------------------------------------------------------

getData.OnServerInvoke = function(Player : Player)
	local UserId = Player.UserId

	if Data[UserId] ~= nil then
		return Data[UserId]
	else
		return DataExample
	end
end

------------------------------------------------------------------
1 Like

I’ve gotten this error before and it was due to storing too much data in a single table. You could try saving the data with a different table hierarchy and see if it works.

1 Like

how could i do this? I don’t know

1 Like

Use profileservice its saves time

1 Like

But, i dont know how to use that, i dont understand

1 Like

Uhm, i setup this, but now getting error “Data stores can only accept valid UTF-8 characters. API: UpdateAsync, Data Store: Data”

1 Like

That means that it can only store numbers values, So like it cant store objects or folders or stuff like that

but im not storing objects, only values and tables like [‘123’] = {}

1 Like

OH I FIXED THAT, I DELETED OS.TIME, yaaaay

1 Like

Are you trying to save colors or vector3?

OH i was kinda late but great so yeah

1 Like