Trouble with saving a table, Please help!

I have a LocalScript that saves an item to a table. It then calls a script to save the items. When a player is added another LocalScript is called to put guis in a players playergui with their text being the saved value. This works fine except it only loads the last item that was put in the table.

Here is the LocalScript that is fired by RemoteEvent when a player joins:


local times = 0
local player = game.Players.LocalPlayer
local frame = player.PlayerGui.Inventory.Frame

game.ReplicatedStorage.LoadItems.OnClientEvent:Connect(function(PlayerData)
	for i,v in pairs(PlayerData) do
		print(i)
		if frame.Item1.Visible ~= true then
			frame.Item1.Text = v
			frame.Item1.Visible = true
		else
			local d = frame.Item1:Clone()
			d.Text = v
			d.Position = UDim2.new(.097 * (i*2),0,.08 * times,0)-- Might need to change
			d.Parent = frame.Item1.Parent
			if (i*2) == 10 then
				i = 0
				times = times + 1
				d.Position = UDim2.new(.097 * (i*2),0,.08 * times,0)
			end
		end
	end
end)
1 Like

I suggest you put some more prints in and even a long wait so you can inspect the situation in explorer as it happens. Like in slow motion.

Chances are that this code isn’t the issue and it is the first instance of putting them into a table. Include all of the code that could be an issue.

I am fairly sure the issue is to do with the for i,v loop. What is happening is that it is looping through the table and as it loops through the table the text keeps changing until it gets to the last item in the table.
Example:

local Table = {
	"One",
	"Two",
	"Three",
}

for i,v in pairs(Table) do
	script.Parent.Text = v
end

-- TextLabel text:
-- Three

Three is only displayed on the TextLabel because that is the last item the loop gets.

is the frame where your items are located? if so try doing

frame[i].Text = v
frame[i].Visible = true

this just makes sure that all the items in the frame are defined with a value.
if the frame doesnt have more than 1 item but the data does try to instance a new frame (or clone a frame prefab) and define the text of that.

EDIT
it’s not the best technique to chech the visiblity bool to make a new frame. just loop through the playerdata and instanciate the frame.item1 prefab as a standard and define it

game.ReplicatedStorage.LoadItems.OnClientEvent:Connect(function(PlayerData)
	for i,v in pairs(PlayerData) do
              local ItemPrefab = frame.Item1:Clone()
              ItemPrefab.Parent = frame
              ItemPrefab.Name = v
              ItemPrefab.Visible = true
        end
end