Help with fixing data stores

Hello! Im currently trying to save data of all int values under a folder, and im not sure how to go about that, and im getting an error that I dont understand, the rror line is commented in the code
code:

local serverStorage = game:GetService("ServerStorage")
local InvTemplate = serverStorage:WaitForChild("InvTemplate")

local DTS = game:GetService("DataStoreService")
local MainDataStore = DTS:GetDataStore("TestDataStore")

function saveData(plr)
	local plrUserId = plr.UserId

	for i, v in pairs(plr:WaitForChild("Inventory"):GetChildren()) do
		local data = v

		local success, errormessage = pcall(function()
			local data = MainDataStore:GetAsync(plrUserId)
		end)

		if success then
			print("Saved ".. plr.Name.. "'s data successfully")
			v.Value = data
		else
			print("Their was an error saving ".. plr.Names.. "'s data")
			warn(errormessage)
		end

		MainDataStore:SetAsync(plr.UserId,  -- giving an error here
			data)
	end
end

local function setupInv(player)
	local inv = player:WaitForChild("Inventory")
	local playerGui = player:WaitForChild("PlayerGui")
	local MainGui = playerGui:WaitForChild("Inventory")
	local invGui = MainGui:WaitForChild("InvUI")
	
	for i, item in pairs(inv:GetChildren()) do
		local itemGui = invGui.Templates.Item:Clone()
		itemGui.Name = item.Name
		itemGui.ItemName.Text = item.Name
		itemGui.ItemAmount.Text = item.Value

		if item.Value > 0 then
			itemGui.Visible = true
			itemGui.Parent = invGui.ItemList
		else
			itemGui.Visible = false
			itemGui.Parent = invGui.ItemList
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local gameData = InvTemplate:Clone()
	gameData.Name = "Inventory"
	gameData.Parent = player
	
	local plrUserId = "Player_".. player.UserId

	for i, v in pairs(gameData:GetChildren()) do
		local data
		local success, errormessage = pcall(function()
			data = MainDataStore:GetAsync(plrUserId)
		end)


		if success then
			print("Loaded ".. player.Name.. "'s data successfully")
			v.Value = data
		else
			print("Their was an error loading ".. player.Names.. "'s data")
			warn(errormessage)
		end
	end
	
	setupInv(player)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	saveData(plr)
end)

Error:

104: Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters.

The first issue I can see with this is that you are trying to store roblox objects in the data stores which is not going to work, the error is telling you that you can only store numbers and strings which are the limits of the roblox data stores.

Creating a system like this would require you to use tables to store all the different values which also leads into using HttpService:JSONEncode and Decode (HttpService | Roblox Creator Documentation) to convert said table into a string when setting data and visa verse when getting the data from the data stores which can be stored in the roblox data stores.

It’s quite late for me and I don’t think I could edit the code to make it functional at the moment but I suggest you look into tables and storing them into data stores, if you would like someone else to help you recreate the code I would add some images of how you are storing the different Int Values in the Inventory folder so they can suggest a way of storing the different values in a table

1 Like

Thanks for the in-depth response, ill look into tables and HTTPService, here is an image of how the values are stored (int values) 2021-08-05 19_56_54-Framework - Roblox Studio

This code should work for you however I would still looking into this yourself as it’s something that will come in very useful later in what you do, I’ve made some edits to your code but it shouldn’t affect it.

local serverStorage = game:GetService("ServerStorage")
local InvTemplate = serverStorage:WaitForChild("InvTemplate")

local DTS = game:GetService("DataStoreService")
local Http = game:GetService("HttpService")
local MainDataStore = DTS:GetDataStore("TestDataStore")

function saveData(plr)
	
	local plrUserId = "Player_"..plr.UserId
	
	local data = {}

	for i, v in pairs(plr:WaitForChild("Inventory"):GetChildren()) do
		
		data[v.Name] = v.Value
		
	end

	local Encoded_Data = Http:JSONEncode(data)
	
	local Success, Error_Message = pcall(function()
		
		MainDataStore:SetAsync(plrUserId, Encoded_Data)
		
	end)
	
	if Success == true then
		
		print("saved data")
		
	else
		
		print("Error: "..Error_Message)
		
	end	
	
end

local function setupInv(player)
	local inv = player:WaitForChild("Inventory")
	local playerGui = player:WaitForChild("PlayerGui")
	local MainGui = playerGui:WaitForChild("Inventory")
	local invGui = MainGui:WaitForChild("InvUI")

	for i, item in pairs(inv:GetChildren()) do
		local itemGui = invGui.Templates.Item:Clone()
		itemGui.Name = item.Name
		itemGui.ItemName.Text = item.Name
		itemGui.ItemAmount.Text = item.Value

		if item.Value > 0 then
			itemGui.Visible = true
			itemGui.Parent = invGui.ItemList
		else
			itemGui.Visible = false
			itemGui.Parent = invGui.ItemList
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local gameData = InvTemplate:Clone()
	gameData.Name = "Inventory"
	gameData.Parent = player

	local plrUserId = "Player_".. player.UserId
	
	local User_Data = nil
	
	local Success, Error_Message = pcall(function()
		
		User_Data = MainDataStore:GetAsync(plrUserId)	
		
	end)

	if Success == true then
		
		if User_Data ~= nil then
			
			local Decoded_User_Data = Http:JSONDecode(User_Data)
			
			for Index, Value in pairs(Decoded_User_Data) do
				
				if gameData:FindFirstChild(Index) then
					
					gameData:WaitForChild(Index).Value = Value
					
					print("set"..Index.." to "..Value)
					
				end
				
			end
			
		else
			
			print("players data is nil setting default values")
			
			for Index, Value in pairs(gameData:GetChildren()) do
				
				if Value:IsA("IntValue") then
					
					Value.Value = 0
					
				end
				
			end
			
		end
		
	else
		
		print("failed to load data for "..player.Name)
		
	end

	setupInv(player)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	saveData(plr)
end)
1 Like

You don’t need to use HttpService to store tables, so long as they aren’t mixed and aren’t cyclic, they can be stored in datastores as they are.

1 Like

I didn’t know that, thanks for telling me