I cant seem to get datastoring a string to work

I am making a game where you have an inventory and when you leave the game it should make a string like this “Wood,6,Stone,9” but it doesnt give me an error or save at all. i looked everywhere to see what it was but i just cant find whats wrong. and when you join its supposed to decode the string back into items but i havent gotten to that point because i havent gotten it to save

local DataStore = game:GetService("DataStoreService")
local InventoryData = DataStore:GetDataStore("Inventory")
local items = require(game.ReplicatedStorage.Module.Blocks)
game.Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Wait()
	--for i, item in pairs(items)  do
		local datahas, errorgettingwompwomp = pcall(function()
			data = InventoryData:GetAsync(player.UserId.."Inv")
		end)
		if data then
			local datatable = data:split(",")
			print(datatable)
			print(data)
			print("Joined Player has a saved inv")
			for i = 1,datatable do
				if i % 2 == 0 then
				else
					local value = Instance.new("NumberValue")
					value.Name = datatable[i]
					value.Value = datatable[i+1]
					value.Parent = player:FindFirstChild("Resources")
				end
			end
		else
			print("Player has no saved inv")
		end
	--end
	workspace.Player[player.Name].CanDig.Value = true
	
end)
game.Players.PlayerRemoving:Connect(function(player)
	if not player.Resources then
		print(player.Name.."Nope")
	else
		print("Has inventory")
		local inventory = ""
		for i,v in pairs(player.Resources:GetChildren()) do
			if v:IsA("NumberValue") then
				if v.Value then
					inventory = inventory..v.Name..","
					inventory = inventory..v.Value..","
				end
			end
		end
		inventory = tostring(inventory)
		local dingdingset, wompwompset = pcall(function()
		InventoryData:SetAsync(player.UserId.."Inv",inventory)
		end)
		print(inventory)
		print(dingdingset,wompwompset)
		if dingdingset then
			print("Saved all data")
		end
	end
end)
2 Likes
SetAsync(player.Backpack:GetChildren())

Why does simply this not work?

I wouldn’t recommend using strings for saving in this occasion, as you probably won’t need to worry about a higher data use.

Instead of strings, use tables, or dictionaries to get some benefits, such as:

  • Less confusing

  • Easier to work with

  • Way more organized

“But what is the problem with the code?”

Well, from what i analized, it seems to be this part:

local datahas, errorgettingwompwomp = pcall(function()
	data = InventoryData:GetAsync(player.UserId.."Inv")
end)
if data then
	-- code here
end

As you didn’t declare the variable “data” before

Here is it fixed:

local data = InventoryData:GetAsync(player.UserId.."Inv")
if data then
	local datatable = data:split(",")
	print(datatable)
	print(data)
	print("Joined Player has a saved inv")
	for i = 1, #datatable do
		if i % 2 ~= 0 then
			local value = Instance.new("NumberValue")
			value.Name = datatable[i]
			value.Value = datatable[i+1]
			value.Parent = player:FindFirstChild("Resources")
		end
	end
else
	print("Player has no saved inv")
end

Strings aren’t the issue. I tried using tables and they just didn’t work. Also I’m having issues saving the code and not loading it because I haven’t been able to save it so that means I can’t load data until I get the saving to work. I just feel like my Roblox studio is broken or something.

That’s not the type of inventory I’m doing.

Sorry for not being able to reply, i did test your code, and after the fix i was able to save and load data without changing anything more than the loading.

Did you activate API Services for studio at your game’s settings?

Yea I did turn it on I don’t know why it would not be working

what happends is that when the last player lefts the game, the server turns off before doing the save, look at this code example of roblox DataModel | Documentación del Centro de creación de Roblox

Maybe just doing

game:BindToClose(function()
	task.wait(30)
end)

could fix it

I guess I could give that a shot I’ll try it out

Earlier I made it so for each item it creates a different key with the player id and item name and that worked but then it stopped working so it might be a problem with the server shutting down but idk I’ll try it out

ok i got it working i dont know what the issue was before because it should have worked but heres what i did and im not sure if the bind to close did anything or not but it doesnt hurt

local DataStore = game:GetService("DataStoreService")
local InventoryData = DataStore:GetDataStore("Inventory")
local items = require(game.ReplicatedStorage.Module.Blocks)
game.Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Wait()
	--for i, item in pairs(items)  do
	
		local data = InventoryData:GetAsync(player.UserId.."Inv")
		
		if data then
			print(data)
			print("Joined Player has a saved inv")
			for i, dataval in data do

				local value = Instance.new("NumberValue")
				value.Name = dataval["Name"]
				value.Value = dataval["Count"]
				value.Parent = player:FindFirstChild("Resources")
			
			end
		else
			print("Player has no saved inv")
		end
	--end
	workspace.Player[player.Name].CanDig.Value = true
	
end)
game.Players.PlayerRemoving:Connect(function(player)
	if not player.Resources then
		print(player.Name.."Nope")
	else
		print("Has inventory")
		local inventory = {}
		for i,v in pairs(player.Resources:GetChildren()) do
			if v:IsA("NumberValue") then
				if v.Value then
					local data = {}
					data["Name"] = v.Name
					data["Count"] = v.Value
					table.insert(inventory,data)
				end
			end
		end
		--local dingdingset, wompwompset = pcall(function()
		InventoryData:SetAsync(player.UserId.."Inv",inventory)
		--end)
		print(inventory)
		--print(dingdingset,wompwompset)
		--if dingdingset then
		print("Saved all data")
		--end
	end
end)

game:BindToClose(function()
	task.wait(5)
end)

i think the pcall bugs it
and heres what im working on if anyones interested

Keep the pcall in case the script errors.

local Data
local success, errorm = pcall(function()
    data = InventoryData:GetAsync(player.UserId.."Inv")
end)

Also, keep the BindToClose so that the server has time to save the data when you leave.

im keeping the bindtoclose and i might add pcall but a good developer friend of mine got heated after hearing that i used pcall idk why. and also does pcall just tell you what the error is and doesnt really do anything about it

The pcall basically allows you to handle the error if something goes wrong. By the way, it is bad practice to not use a pcall with datastores.

yea i understand but what do you mean handle does that just mean to tell you if it errors and what it is or like does it try again

Kind of. I am not great at explaining things so I would read this post if you want to know what pcalls are for: Pcalls - When and how to use them

well i know how they work
local yes, error = pcall
the yes is if its a success
so if yes then do code
and the error is the error code so if not yes print(error) would display the error and other than that i think thats it