How do you prevent indexing a function?

In my code, it goes through properties, but I have some functions inside of my module script. I want to know if there’s a way to detect when the thing is a function or not.

--Module Script:
local Weapons = {
	{
		--Contains weapon data in actual code.
	},
	{
		--Contains weapon data in actual code.
	},
	{
		--Contains weapon data in actual code.
	}
}

function Weapons.Reassemble(Array)
	--Function code is not relevent
end

function Weapons.FixGaps (FixArray)
	--Function code is not relevent
end

return Weapons
--Local Script:
for _, Weapon in pairs(ItemDictionary) do --Adds all the items to the weapon shop.
		if Weapon then
			if Weapon.Purchasable == true then
				--Code
			end
		end
	end

ReplicatedFirst.Game_Client:707: attempt to index function with 'Purchasable'

use

if type(Weapon) ~= "function" then
   -- proceed with loop
end

This basically checks the type of the current object

1 Like

Functions can’t be indexed, hence why the message says attempt to index.

You have a mixed table anyway, so just use ipairs to traverse only the array part of it.

for _, Weapon in ipairs(ItemDictionary) do --Adds all the items to the weapon shop.
		if Weapon then
			if Weapon.Purchasable then
				--Code
			end
		end
	end
2 Likes

I don’t think this would solve my problem, but I will keep ipairs in mind when I code something next. Thanks for the insight!

Except it does, since Weapons.Reassemble and Weapons.FixGaps are part of the dictionary part, which ipairs doesn’t traverse over

Okay, that makes sense, I’ll mark yours as the solution since it helps out for future development on the code.