Table expected, got number when trying to create notes from a JSONDecode table

Hello,

I’m currently writing a note script, which will create notes to allow players to play songs. However, I’m getting the error “table expected, got number”. Here is my code & table structure for reference:

local notes = songJSON[tostring("notes")]
local speed = songJSON[tostring("speed")]

for v,i in pairs(notes) do
	for b,o in pairs(v) do -- error here
		for n,p in pairs(b["sectionNotes"]) do
			createNote(p[1], p[2], p[3], speed)
		end
	end
end

Table structure:
image

[1] = timing
[2] = direction
[3] = type(?)

Any idea why this is happening? I tried using ipairs, as well.

Thanks,
p0s_0.

1 Like

In your case, v is the index of the current iteration and i is the value in that index, you might have to swap those around

1 Like

Now I’m getting the error “invalid argument #1 to ‘pairs’ (table expected, got nil)”.

Try printing i and v and see what they output? Whats your code right now after the change?

for i,v in pairs(notes) do
	print(tostring(i)) -- prints 1
	print(tostring(v)) -- prints table: 0xaf7aef1aa514c1fd
	for b,o in pairs(v) do
		for n,p in pairs(b["sectionNotes"]) do
			createNote(p[1], p[2], p[3], speed)
		end
	end
end

So it should be working fine, But I think your issue is in the other pairs loop, try this for what you’re trying to do?

for i,v in pairs(notes) do
	for n,p in pairs(v["sectionNotes"]) do
		createNote(p[1], p[2], p[3], speed)
	end
end
1 Like