Why am I getting the error: attempt to index function with "ID"?

Achieve

I’m attempting to have it where a function reads all of the tables values and check for a players UserId and if it finds it in the said area, it will print that it completed through it’s function.

Issues

My issue is that i’m apparently… attempt to index function with "ID" image

Solutions

I’ve tried having it do a check with no table in the ID area but that failed and gave me the same error.

Script

local ChatTagModule = {
	["AlexTatums Tag"] = {
		ID = {571909198},
		Description = "Description.",
		Function = function(plr)
			print("Completed")
		end,
	}
,
        ["ETag"] = {
		ID = {2314546},
		Description = "Description.",
		Function = function(plr)
			print("Completed")
		end,
	}	
}

function ChatTagModule.CheckPlayer(plr)
	for _,ModuleFunction in pairs (ChatTagModule) do
		print(plr.UserId)
		if table.find(ModuleFunction.ID,plr.UserId) then -- This is the line that gives the error.
			ModuleFunction.Function(plr)
			return true	
		else
			return false
		end
	end
end

return ChatTagModule

Thank you for any help you can give me.

Where is line 87? Is it the table.find line? If so you may have to do this ModuleFunction.ID[1]

Have you forgot to index “AlexTatums Tag” before “ID”?

He looped through the table so “AlexTatums Tag” is “ModuleFunction”

I think the issue is that it’s somehow getting a function somewhere, it could either be the Function in those tables or the CheckPlayer Function. Try removing the Function parts of the table entries and see if the error persists

This I’m not sure if this is related but shouldn’t the . be a :? Correct me if I’m wrong

The reason you’re getting that error is because you’re assigning a function within the ChatTagModule table called CheckPlayer, so when you loop through the contents of ChatTagModule you’re iterating over a function which doesn’t contain any data that you are trying to index, which in this case would be ID. To solve this, you should create a separate table within the ChatTagModule table which contains the tag data to iterate over.

local ChatTagModule = {
	TagData = {
		["AlexTatums Tag"] = {
			ID = {571909198},
			Description = "Description.",
			Function = function(plr)
				print("Completed")
			end,
		}
		,
		["ETag"] = {
			ID = {2314546},
			Description = "Description.",
			Function = function(plr)
				print("Completed")
			end,
		}
	}
}

function ChatTagModule.CheckPlayer(plr)
	for _,ModuleFunction in pairs (ChatTagModule.TagData) do
		print(plr.UserId)
		if table.find(ModuleFunction.ID,plr.UserId) then -- This is the line that gives the error.
			ModuleFunction.Function(plr)
			return true	
		else
			return false
		end
	end
end

return ChatTagModule
2 Likes

This would be line 87 in my version of the script.

Try ModuleFunction.ID[1] unless you already have.
Also are you going to have multiple ID’s, if not then you don’t need to have them in the table.

Edit: You don’t even need the table.find, cant you do

if ModuleFunction.ID == plr.UserId then

This is interesting and quite nice to see. I have not seen a function be used inside of a table.