Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[PLEASE SOMEONE ANSWER] How to save/load a table?

Please I have been asking and searching for so long. PLEASE SOMEONE ANSWER THIS!!! I NEEED TO KNOWWWWWWWWWWW!!!!!!

Anyways folks... I have an inventory system where the player's folder is in ServerStorage and there are tools in ReplicatedStorage called Sword and Iron Sword. Players start off with a normal sword and if they touch a part they get the iron sword(for testing). Here is my server script in workspace:

local ds = game:GetService("HttpService")
local das = game:GetService("DataStoreService")
local played = das:GetDataStore("plAyeD")
local invs = das:GetDataStore("Inventory")
local function load(plr)
	local f = Instance.new("Folder",game.ServerStorage)
	f.Name = tostring(plr.Name)
    local playedbefore = Instance.new("BoolValue",plr)
	playedbefore.Name = "PlayedBefore"
	playedbefore.Value = played:GetAsync(plr.UserId,playedbefore.Value) or false
	wait()
	local inv = {}
	if playedbefore.Value == true then
		print("The player is not new.")
		for i = 500000,0,-1 do
			local s, e = pcall(function()
				inv = {}
				invs:GetAsync(plr.UserId,ds:JSONDecode(inv))
			end)
			if s then
				print("Loaded Data!")
				i = 0
				break
			end
			if e then
				warn(e)
			end
		end
		wait()
		local num = table.getn(inv)
		if num >= 1 then
			for i, v in pairs(inv) do
				local t = table.unpack(inv,i,i)
				local cl = game.ReplicatedStorage:WaitForChild(t):Clone()
				local cll = game.ReplicatedStorage:WaitForChild(t):Clone()
				cl.Parent = plr.Backpack
				cll.Parent = plr.StarterGear
			end
		end
	else
		inv = {"Sword"}
		wait()
		for i, v in pairs(inv) do
			local t = table.unpack(inv,i,i)
			local cl = game.ReplicatedStorage:WaitForChild(t):Clone()
			local cll = game.ReplicatedStorage:WaitForChild(t):Clone()
			cl.Parent = plr.Backpack
			cll.Parent = plr.StarterGear
		end
	end
end

local function save(plr)
    local inv = {}
	for i, v in pairs(game.ServerStorage:WaitForChild(plr.Name):GetChildren()) do
		table.insert(inv,#inv + 1,v.Value)
		v:Destroy()
	end
	wait()
	for i = 50000,0,-1 do
		local s, e = pcall(function()
			invs:SetAsync(plr.UserId,invs:JSONEncode(inv))
		end)
		if e then
			warn(e)
		else
			break
		end
	end
	if plr:WaitForChild("PlayedBefore").Value == false then
		for i = 50000,0,-1 do
			local s, e = pcall(function()
				played:SetAsync(plr.UserId,true)
			end)
			if e then
				warn(e)
			else
				break
			end
		end
	end
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
   load(plr)
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
    save(plr)
end)
game:BindToClose(function()
    for _, client in ipairs(game.Players:GetPlayers()) do
		coroutine.wrap(save)(client)
    end
end)

Here is my script inside the part:

local db = {}
script.Parent.Touched:Connect(function(hit)
	if hit:FindFirstAncestorOfClass("Model") then
		local char = hit:FindFirstAncestorOfClass("Model")
		if game.Players:GetPlayerFromCharacter(char) then
			local plr = game.Players:GetPlayerFromCharacter(char)
			if db[char] == true then
			else
				if plr.StarterGear:FindFirstChild("Iron Sword") then
				else
					db[char] = true
					local cl = game.ReplicatedStorage:WaitForChild("Iron Sword"):Clone()
					local cll = game.ReplicatedStorage:WaitForChild("Iron Sword"):Clone()
					cl.Parent = plr.Backpack
					cll.Parent = plr.StarterGear
					wait(2)
					db[char] = nil
				end
			end
		end
	end
end)

Here is my local script in StarterGUI:

local plr = game.Players.LocalPlayer
local area = plr:WaitForChild("StarterGear")
wait()
if #area:GetChildren() > 0 then
	for i, v in pairs(area:GetChildren()) do
		game.ReplicatedStorage.UpdateInventory:FireServer(v.Name)
	end
end
area.ChildAdded:Connect(function(obj)
	game.ReplicatedStorage.UpdateInventory:FireServer(obj.Name)
end)

Here is my server script in ServerScriptService:

game.ReplicatedStorage:WaitForChild("UpdateInventory").OnServerEvent:Connect(function(plr,tool)
	local f = game.ServerStorage:WaitForChild(plr.Name)
	local st = Instance.new("StringValue",f)
	st.Name = "Item"
	st.Value = tool
end)

When I join at first(I change the played DataStore to test) it gives me the Sword. When I touch the part it successfully gives me the tool and adds the value to the folder. But when I leave and rejoin There is nothing in my inventory. The load call printed: "Loaded data!" although it didn't. I think it's something got to do with the saving of it. (I have tested this in Studio and in-game).

(No Errors or Warnings)

Thanks in advance!

2 comments

0

As I see, line 60-69 is for saving, not for getting the values after you load in the game and setting the tools from the saved table to the player's backpack.

0

1 answer

I can't give you the exact code, because I don't really understand your script, but I don't see you getting a tool AND PLACING in the player's inventory. I've seen a guy give the same question(not with tools though), but setting the value to the table value worked. So basically, you could do this:

local tooltable = http:JSONDecode(tablefromDataStore)
for i,v in pairs(tooltable) do
	if game.ServerStorage:FindFirstChild(v.Name) then -- if the tools are in serverstorage
		v.Parent = plr.Backpack
end

This should help you.

1 comment

0

I did have it: Line 60-69 of the script in workspace.

This discussion is preserved as it was. New answers, comments, and votes are closed.