Printing table contents not working

Ok, so basically I am trying to loop in a table using a for loop, for.

The problem is, when I print it out, it prints 1 then a really long table ID after it; I am trying to save data. How do I fix this?

1 Like

Can you post your code so we can help you?

1 Like

The remote event is hooked up to a local script, which runs, and saves the data; but I can’t print the data.

game.ReplicatedStorage.SaveData.OnServerEvent:Connect(function(player)
	for i,v in pairs(game.Workspace.Plots:FindFirstChild(player.PlotOwned.Value):GetChildren()) do
		if v:IsA("Part") and v then
			local data = {}
			
			table.insert(data, {Type = v.Name, CFrame = tostring(v.CFrame),Color = tostring(v.Color)})
			local id = "Player_" .. player.UserId
			
			local success, err = pcall(function()
				
				partstore:SetAsync(id,data)
			end)
			
			
			if success then
				for i, v in pairs(data)	 do
					print(v)
				end			
				
			else
			print(err)
			end
			
		end
	end
end)
1 Like

Just for a test, but try printing the data individually, like print(data[1])

1 Like

Strange, it’s printing all nil.

1 Like

What if you try saving the data individually, something like this?

game.ReplicatedStorage.SaveData.OnServerEvent:Connect(function(player)
    local data = {}
	for i,v in pairs(game.Workspace.Plots:FindFirstChild(player.PlotOwned.Value):GetChildren()) do
		if v:IsA("Part") and v then
			table.insert(data, v.Name)
            table.insert(data, tostring(v.CFrame))
            table.insert(data, tostring(v.Color))
			local id = "Player_" .. player.UserId
		end
    end
    local success, err = pcall(function()
		partstore:SetAsync(id,data)
    end)
			
	if success then
		for i, v in pairs(data) do
			print(data[i])
		end			
	else
	   print(err)
    end
end)

I’m not sure if this would fix your issue, but depending of the result we could work this out.

I’m trying to save the properties of a part. So I will try that and see what happens.

1 Like

Let me just get into studio as well for a sec, writing everything in the browser is a bit annoying.

I think i might have the solution for your issue, give me a while.

Also, i forgot to ask, but what exactly is “partstore” in partstore:SetAsync(id,data) ?

Ok. Thanks I’ll be back soon .

It’s a data store. I am setting the data.

Alright, that’s good to know. I’ll see what i can do with your script, and then explain the changes about what i changed on them.

1 Like
game.ReplicatedStorage.SaveData.OnServerEvent:Connect(function(player)
	
	local data = {}
	
	for i,v in pairs(game.Workspace.Plots:FindFirstChild(player.PlotOwned.Value):GetChildren()) do
		if v:IsA("Part") and v then
			local stringValue = Instance.new("StringValue", player) -- creates a new string value, simply for saving the data
			stringValue.Name = v.Name
			stringValue.Value = (v.Name.." "..tostring(v.CFrame).." "..tostring(v.Color))
			print(stringValue.Value) -- prints the value of the string value, aka the part properties and name
			table.insert(data, stringValue.Value) -- insert the part properties into the table
			stringValue:Destroy() -- we don't need it anymore, so it's now gone
		end
	end
	
	local id = "Player_" .. player.UserId
	local success, err = pcall(function()
		partstore:SetAsync(id,data) -- saving data
	end)

	if success then
		for i, v in pairs(data) do
			local values = string.split(v," ")
			print(values[1]) --part name
			print(values[2]) --part cframe
			print(values[3]) --part color
		end			
	else
		print(err)
	end
end)

I have reworked a bit of the script and also explained what the new additions does. Hopefully this can fix your issue.

Holy crap! Thanks dude. Your an animator and I’m a scripter. I gotta get better; thanks!

1 Like

No worries, glad i could help you! If you need anything else, just mention me and i’ll come back to check.