Is there GetChildren() for Tables?

Hey Devs,

I am fairly new to scripting in general and even more so to tables.

I am trying to figure out if there is a for i, v in pairs (table:GetChildren() do alternative that will actually work for tables.

Basically I need to be able to get each of the values that are inside of a table and do run a function for each different value.

So far I know that GetChildren() does not work because a table is not an instance. I have also tried table.foreach and table.foreachi as I would have assumed that these would have worked each value within the table, but this is the result of that:

With only one value, I noticed it continuously added extra characters to the value:
image
image
image

With multiple values, I realized that it was just taking the entire table, and running the function or that:
image

The function is supposed to be ran on each value, and is supposed to index them with the in-game values.

Basically, I am just looking for a GetChildren for tables. If anyone can help me with this, please do! Thank you!

2 Likes

GetChildren actually just returns a table :slight_smile:

So you can just do for i,v in pairs(myTable)

7 Likes

1st of all, WOW I really did not know that, so thank you!

2nd, now my code is only running up to my first table. Here is a section of my code, where you can see print functions. This won't print obviously does not print, and I am assuming that is because the loop from the first section/table does not stop running, even though there is only 1 value with that table.

Summary
	print("this will print")
		
		local anims = {} 
		table.insert(anims, stored[7])
		if #anims > 0 then
			print("passed anim check 1")
			for i, v in pairs(anims) do
				print(v)
				if v == "[]" then return end
				local item = animationsinventory:FindFirstChild(v)
				if item then
					print(item) -- does not print because v does = []
					item.Value = true
				else
					print("created anim item")
					local bool = Instance.new("BoolValue")
					bool.Name = v
					bool.Value = true
					bool.Parent = animationsinventory	
				end
			end
		end	
		
		print("this wont print")

		local effects = {}
		table.insert(effects, stored[8])
		if #effects > 0 then			-- none of this happens
			for i, v in pairs(effects) do
				if v == "[]" then return end
				local item = effectsinventory:FindFirstChild(v)
				if item then
					item.Value = true
				else
					local bool = Instance.new("BoolValue")
					bool.Name = v
					bool.Value = true
					bool.Parent = effectsinventory	
				end
			end
		end

image

2 Likes

Are you seeing any errors in the output?

And how are you creating stored?

Are you ever JSONDecode-ing your data?

pairs will only go through each element of the table once, so it’s probably not getting stuck in the loop.

1 Like

this might be your problem, return will end the entire function this is in

example
local hello = nil
local function func()
    local t = {}
    table.insert(t,hello)
    if #t > 0 then
        for _, v in pairs (t) do
            if not hello then return end -- as hello is nil this will return ending the function
        end
    end
    print("this will never print") -- as the function as been returned before this point this will never print
end

func()

what i believe your looking for would be continue

if v == "[]" then continue end

try seeing if this will fix it

2 Likes

@56ytr56 Continue fixed the issue where the code stopped after the first table

@nicemike40

I get no errors related to the saving/loading of data, but I believe that I might be having an issue with saving my tables via JSONEncode-ing.

I have not been JSONDecode-ing any of the tables as I honestly did not know anything about it; I just seen that encoding was on the only way to save tables didn’t think about having to decode them after.

I am currently having an issue where this code:

Summary
local phones = {}
		table.insert(phones, stored[9])
		print(phones)
		if #phones > 0 then
			for i, v in pairs(phones) do
				print(v)
				if v == "[]" then continue end
				local item = phoneinventory:FindFirstChild(v)
				if item then
					item.Value = true
					print(item)
				else
					local bool = Instance.new("BoolValue")
					bool.Name = v
					bool.Value = true
					bool.Parent = phoneinventory	
				end
			end
		end	

Via the print functions, prints each saved Value as one object.

Print(v):
image

Print(table):
image

I am assuming that this is being rooted in the fact that I have not been decoding the tables?