Table.find() Not Working Correctly

I’m trying to make a system for my DataStores to make sure that a player’s data is fully intact in case I add new keys to the table and new ones need to be created. Right now, I’m having an issue with using table.find().

I am trying to search if the key from table ExampleData exists within the player’s data (referenced as table Data in the code), and if not it would return nil and warns a message. The problem is, it is always returning nil despite me printing Data and seeing that the key being checked for exists.

If anyone could explain why this is happening or a correct way to achieve this, please leave a reply.

Here’s the snippet from the code:

			for v, i in pairs(ExampleData) do
				local Search = (table.find(Data, tostring(v)))
				if Search == nil then
					warn("Missing data for " .. tostring(v) .. ", generating replacement.")
				end
			end
1 Like

Try printing out what tostring(v) is.

It seems fine to me. Here’s what gets printed in the output:

As you can see, the Money key already exists in the data, and tostring(v) is equal to Money, but Search returns nil.

find only works with numerical indices, not dictionaries. If it finds the specified element, it’ll return the numerical index where it was at in the table.

If you’re trying to determine if a specific key exists, just use this syntax instead:

if Data.Money then
--//money exists
end
1 Like

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