Invalid key to 'next' after adding items to array

For some strange reason, the following simple usage of next() in finding the next value is causing the error in the title.

local validObjects = {}
local validNames = {}

	for name, styleObject in pairs(PlayerCombatStyles) do
		if styleObject.WeaponClass == CurrentCombatStyle.WeaponClass then
			validObjects[name] = styleObject
			table.insert(validNames, name)
		end
	end
		
		--[[ This does not seem to affect it??
		table.sort(validNames, function(a,b)
			return a < b
		end)
		]]
		
	local nextStyleName, combatStyleValue = next(validNames, CurrentCombatStyle.Name)

What am I doing wrong? Thanks for the help!

If this is a dictionary, dictionaries don’t have an order to them and can’t be sorted.

What is CurrentCombatStyle.Name and what is the content of the validNames table at the time this error occurs?

Right now I use table.sort() to sort the indices of the dictionary (which are strings contained in the validNames array) so that they are alphabetical.

CurrentCombatStyle.Name’s value is a string (this code won’t run if this is nil), and the validNames array should contain the string indices of the dictionary (which are names of combat styles).

Hope this helps!

Oh yeah I see. What are you trying to do with that line? next is generally reserved for working with dictionaries and you pass it the key, not the value.

I can see here that validNames is an array, so CurrentCombatStyle.Name (which you say is a string) will indeed cause an error with next because next expects in this case a valid integer key for the validNames array.

1 Like

Ah I see. I’ll replace next() with something to do with table.find(). Thanks for the help!

1 Like

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