Saving any data

Hello every one! I hope every one is having an amazing summer! I am working on a game with an inventory system, wich I’m now trying to save in a data store. I came up with a script to save data from any kind of tool you picked up, but somehow I have a problem.

There are no errors but every time I run the script (below), it first prints the values in the data table wich are saved, but when the values need to be set to the inventory it just sets to 0. I already tried a couple of things but nothing seems to do the job. Can someone help me? Here is the script and output:

local DataStoreService = game:GetService(‘DataStoreService’)
local myDataStore = DataStoreService:GetDataStore(‘DataStore’)

game.Players.PlayerAdded:Connect(function(player)
local inventory = Instance.new(‘Folder’, player)
inventory.Name = ‘Inventory’

for i, v in pairs(game.ReplicatedStorage.inventoryItems:GetChildren()) do
	local var = Instance.new('IntValue', player.Inventory)
	var.Name = v.Name
end

local playerUserId = "Player_"..player.UserId
local data
local success, errorMessage = pcall(function()
	data = myDataStore:GetAsync(playerUserId)
	print(table.unpack(data))
end)

print(table.unpack(data))
if success then
	print(table.unpack(data))

	for i, v in pairs(player.Inventory:GetChildren()) do
		v.Value = tonumber(data[v.Name])

		wait()
		print(v.Value)

		if v.Value == 0 then
			print('EMPTY')
		else
			local item = game.ReplicatedStorage.inventoryItems:FindFirstChild(v.Name)
			local stick = item:Clone()
			local place = player.PlayerGui:WaitForChild('InventoryGui')

			stick.Parent = place.Frame
			wait()
			stick.Count.LocalScript.Enabled = true
			print('Copied')
		end

		print(table.unpack(data))
	end
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId
local data = {}

for i, v in pairs(player.Inventory:GetChildren()) do
	local var = v.Name
	var = v.Value
	table.insert(data, var)
end

local success, errorMessage = pcall(function()
	myDataStore:SetAsync(playerUserId, data)
end)

if success then
	print('Saved Successfully!')
else
	print('Saving Error!')
	warn(errorMessage)
end

end)
OUTPUT:

  • when player joins the game:

OUTPUT1

-when player leaves the game:

OUTPUT2

hope some one can help me. Thanks in advance!

1 Like

to handle data with tables, use the JSONEncode() and JSONDecode()

what does this code? I’m quit a noob so I’m sorry

This is an example to that:

local DS = game:GetService("DataStoreService")
local myDS = DS:GetDataStore("DataStore_save1")
local HttpService = game:GetService("HttpService")

game.Players.PlayerAdded:Connect(function(player)
	local inv = Instance.new("Folder")
	inv.Name = "Inventory"
	inv.Parent = player
	
	-- Load Data
	local data = nil
	local success, message = pcall(function()
		data = myDS:GetAsync(tostring(player.UserId).."_key")
		-- Decode and convert it into table/array
		if data then
			data = HttpService:JSONDecode(data)
		end
	end)
	
	if not success then
		warn(message)
	end
	
	-- logic to set the data contents into real values
        -- such as table.unpack(data)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local inv = player:FindFirstChild("Inventory")
	if inv then
		local getData = {}
		for item in inv do
			local itemInfo = {}
			table.insert(itemInfo, {item.Name, item.Value}) -- compile item's info.
			table.insert(getData, itemInfo) -- compile all items into one
		end
		
		-- Encode to make it as a string or JSON
		local Encode = HttpService:JSONEncode(getData)
		
		-- Save
		local success, message = pcall(function()
			myDS:SetAsync(tostring(player.UserId).."_key", Encode)
		end)
		
		if not success then
			warn(message)
		end
	else
		warn("Inventory not found in", player.Name)
	end
end)

Allright I understand thank you so much!

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