Weird for loop issue

Packets.getDatabase.listen(function(data)
	local decoded = HttpService:JSONDecode(data.data)
	local label = addValue("Key")
	local titleClone = ReplicatedStorage.client.assets.interface.DatabaseKeyTitle:Clone()
	titleClone.Parent = Panel.Pages.Database.Content.Keys.Key
	addLabel(data.key, label)
	
	for i, v in decoded do
		if type(v) == "table" then
			print(v)
			local stringBuilder = {}
			for i2, v2 in v do
				print(i2,v2)
			end
		
		else
			local label = addValue(i)
			local titleClone = titleClone:Clone()
			titleClone.Text = string.gsub(i, "_", "")
			titleClone.Parent = label
			addLabel(v, label)
		end
	end
end)

in the line with print(v) it only prints things that is a table (as it should), but in the for loop where I loop v and I do print(i2,v2) it prints everything in decoded, also the stuff that is not a table

I don’t see either of the print statements you’ve mentioned in this code snippet

Oops, I removed them for some reason, I’ve edited the post and added them.

when you loop through v it doesn’t guarantee that every element of v is also a table / not a table. If you want to do that check you would want to make a recursive function. Otherwise I don’t really understand the problem fully and would ask for output samples

I don’t really understand, because as you can see I am checking if v is a table in the first loop, before I loop v. But it’s still doing this for some reason.
image
Here is a snippet from the output. Line 66 is print(i2,v2)

are you certain that those arent tables? cause the logic of the scripts seems to be working

(try to print type(v.parent))

Yes, as you can see e.g “ClockType” is a string, and there’s lots of numbers also.

i see, what does the addlabel do? does it mess with the data?
also i would like to see a print of the type of the v to gurantee that it isnt a table

addLabel() does not mess with the data, and printing v prints this
image

okay so, reading back your question, when you do for i2,v2 loop you just print everything that is inside the table, that means it prints values and variables that contains tables. i dont think it bugs out, to fix it printing stuff that isnt a table just put if type(v2) == “table”

I still don’t understand, because I am checking if v is a table, and then looping v if it is. i2 and v2 is then the indexes and values of v (which should be a table).

you are checking if v is a table indeed but you arent checking if v2 is a table…

the main problem isnt print(v) its printing(i2,v2), whatever the table contains, it can contain another table or values

1 Like

OH, I get it now. It’s printing the values inside of v which also contains tables. I just thought it was printing stuff outside of v.

Thanks though!

1 Like

ye np thats why we were confused, its not printing anything outside. to fix printing every value just put another if type(v2) on the second print amd it should only print tables

tip: never trust data you are getting from, always check the type of data.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.