Need help with datastore serialization

Greetings developers! I’m currently facing an issue with datastores and I’m in desperate need of help

I’m trying to make a round history panel where the player is supposed to view their in-game history and the rounds they’ve played as shown in the image below

image

my issue lies with datastores as when the player rejoins the game the list gets cleared.
I’ve tried following tutorials and serializing data but I don’t think it’s working, because I have no experience with datastores and have never used it before.

here are all the children if that’s going to help, everything in there needs to be saved
image

All help is appreciated, I’ve been stuck on this for the past few hours

1 Like

Could you show your current datastore script, just so we can build on that? Also, where exactly is this datastore script located? Preferably ServerScriptService.

2 Likes
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SaveHistory")

game.Players.PlayerAdded:Connect(function(Player)
	
	if not Player.PlayerGui then
		repeat
			wait(1)
		until Player.PlayerGui
	end
	
	wait(1)
	
	Player.PlayerGui.MainGui.MotherFrame.HistoryFrame.HistoryScroller.ChildrenNumber.Changed:Connect(function()
		
		local PlayerUserId = Player.UserId
		local PlayerData = "Player_TEST6" .. PlayerUserId

		local Data = {}

		for i, v:Frame in ipairs(Player.PlayerGui.MainGui.MotherFrame.HistoryFrame.HistoryScroller:GetChildren()) do
			if v:IsA("Frame") then
				table.insert(Data, {
					v.Parent,
					v.Player1Image.Image,
					v.Player2Image.Image,
					v.Player1DName.Text,
					v.Player2DName.Text,
					v.Player1RName.Text,
					v.Player2RName.Text,
					v.Result.Text
				})
			end

			local Success, Error
			print(Data)
			
			repeat

				Success, Error = pcall(function()

					DataStore:UpdateAsync(PlayerData, function()
						return Data
					end)

				end)
				print("SAVED DATA")
				task.wait()

			until Success

			if not Success then
				warn("Failed To Save Data")
			end

		end
		
	end)
	
end)

local function LoadData(Player)
	
	if not Player.PlayerGui then
		repeat
			wait(1)
		until Player.PlayerGui
	end
	
	wait(1)
	
	local PlayerUserId = Player.UserId
	local PlayerData = "Player_TEST6" .. PlayerUserId
	
	local Success, Error
	local Data
	
	repeat
		
		Success, Error = pcall(function()
			Data = DataStore:GetAsync(PlayerData)
		end)
		
	until Success or not Players:FindFirstChild(Player.Name)
	
	if Success and Data ~= nil then
		print("5")
		for i, obj in ipairs(Data) do
			local Frame = game.ServerStorage.HistoryFrame:Clone()
			Frame.Parent = Player.PlayerGui.MainGui.MotherFrame.HistoryFrame.HistoryScroller
			Frame.Player1Image.Image = obj[2]
			Frame.Player2Image.Image = obj[3]
			Frame.Player1DName.Text = obj[4]
			Frame.Player2DName.Text = obj[5]
			Frame.Player1RName.Text = obj[6]
			Frame.Player2RName.Text = obj[7]
			Frame.Result.Text = obj[8]
		end
		
		print("LOADED DATA", Data)
		
	end
	
end

Players.PlayerAdded:Connect(LoadData)
Players.PlayerRemoving:Connect(function(Player)
end)

this script is almost a 1:1 copy from a script in a YouTube tutorial, I’m still facing some issues and I’m working on it and yes the script is located in ServerScriptService

this is the error I’m getting for now even though I’m saving a table and not an instance.

This inserts that array into the data table. You probably wanted to insert all of those values into the data table directly.

You must encode your data using HttpService:JSONEncode before storing it.

1 Like

The issue is very likely cause by this


You cannot save instances to the datastore. I can see that when getting the data, you don’t even use it, so you should just be able to remove it

The error message is misleading as you can save arrays to the datastore, the error message is probably just saying you can’t save that array because it has invalid utf-8 characters (in this case, an instance)


Yes, good catch, I did not notice that
However, the way it is coded currently, it does work for this structure. Perhaps it was done like this to add more data later on

Not true, DatastoreService does that under the hood, you can store tables directly, and retrieve tables directly as well

2 Likes

This seems to have fixed it! Thank you both for your help! @Tomi1231 @uvic1rn

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.