Finding certain Variables in a table

Alright, I have a program that saves multiple instances positions and names. When they are saved, they go through a for loop that makes a new table for each part in a data table, that table made for each part in the data table and are called [InsertPartName]…i (Example: Part1, Part2, Part3, etc…), i is how many times the for loop has ran. In that table has tables like position and name. Then I encode it with JSON. Great I saved Data, I decoded the JSON. My problem is how would I get those individual parts named Part1, Part2, Part3? Could I use a for loop that goes through the Data table and has an if statement to find the table I’m looking for?

Heres my concept on finding these variables:

    local Part = game.Workspace.Parts:GetChildren()
    local Data = {
    	NumberofParts = {0},
    	Parts = {

    	}
    }
    for i,v in ipairs(Part) do
        Data.Parts[v.Name..i] = {Position = {X = v.Position.X, Y = v.Position.Y, Z = v.Position.Z}, {Name = v.Name}}
    	Data.NumberofParts = i
    end
     local JsonFile = HttpService:JSONEncode(Data)

     --DataStore stuff here

     --Where I find the specific table I want
     local decodetwo = HttpService:JSONDecode(PlayerData)
	      for i = 1, decodetwo.NumberofParts do
		      print(decodetwo.Parts[Part..i].Name)
	      end

I’m somewhat having a hard time understanding what you’re trying to attempt here, but I believe using a dictionary may be the solution, it allows you to save data in an array and call it by a name/index.

local CoolPeople = {{['Name'] = 'You'}, {['Name'] = 'Me'}}
print(CoolPeople[1]['Name']) -- Prints "You"

http://lua-users.org/wiki/TablesTutorial

1 Like

I agree with you.

The thing is unless parts are all uniquely named, some table / dictionary entries could be unaccessible due to conflicting names if OP is just using part names.

@Fsxfighter265 Why are you storing parts by their names? Are they all uniquely named?

2 Likes

Ignore the names, I did that for testing purposes. Let say if a player makes multiple copies of the same instance. I need to save those multiple instances positions. So I make multiple tables within the data table to have their positions stored in them. I use a for loop for this and added the iteration of the for loop to the tables name so I know what table it is. Now with the player loaded, I need to get all of the data that has those instances. What I think would work if I make a for loop starting at one and stopping at the number of instances there are. (I got the number of instances from the data table). In that for loop, I will find the table named the instances name with the iteration of the loop. Which would look like Part1, Part2, Part3. Then I access those variables inside. My problem is how would I know if its Part1 or Part2. I tried doing this to see what part it is:

    	for i = 1, decodetwo.NumberofParts do
			print(decodetwo.Parts.Part[i].Position.X)
		end
1 Like