Problem with getting amount of ids from table

  1. What do you want to achieve? Keep it simple and clear!
    Topic
  2. What is the issue? Include screenshots / videos if possible!
    Idk, i get only 1 instead of real amount
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I tryed looking devforum

Code:

local Messages = {
	["StoryParts"] = {
		["Beginning..."] = {
			["Messages"] = {
				[1] = "In the world of full black...",
				[2] = "Where nothing never would exist...",
				[3] = "Suddenly appeared... you...",
				[4] = "I can't understand what even are you...",
				[5] = "Guess i need some more information about you...",
				[6] = "Good luck with your goal in this empty world..."
			}
		}
	}
}

function StoryMessages:GetCount(part)
	local length = 0
	for _,count in pairs(Messages["StoryParts"][part]) do
		length += 1
	end
	return length
end

It returns 1, not 6

This is because when you are doing Messages["StoryParts"][part] you are only counting how many things are in the ["Beginning..."] table, assuming that the argument for part is Beginning...". If you’d like to get the amount of stuff within ["Messages"], then replace Messages["StoryParts"][part] with this instead:

Messages["StoryParts"]["Beginning..."][part]
1 Like

That’s right, it should return 1.

for _,count in pairs(Messages["StoryParts"][part]) do
	length += 1
end

Messages["StoryParts"][part] =

["Messages"] = {...}

It iterates over the count itself in [“Beginning…”], it is 1 (Messages)

You need use it:

function StoryMessages:GetCount(part)
	return #Messages["StoryParts"][part]["Messages"] --we return the count in the table itself
end

Oh wait just realised that i’m dumb. Thank you and @ScriptedSuper

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