FindMetaTable returning null on SWEP weapon file. Need help accessing player data

Hello, and thank you for reading my post. I’ve just started learning GMOD lua earlier this week, so I’m sure I must be doing something wrong, but as far as I know, my code seems correct.
The problem I’m having seems to be that the global, FindMetaTable() function is returning NUL for some reason, but I don’t understand why. I’m trying to get the player’s metatable, so I can access it’s SteamID() method and use that to identify the player for when they need to grab the inventory data on the server.

Error:
[ERROR] addons/inventory/lua/weapons/weapon_inventory.lua:56: tried to use a NULL entity!

  1. SteamID - [C]:-1
  2. InitInventory - addons/inventory/lua/weapons/weapon_inventory.lua:56

The error is coming from the self:SetInvDirectory(steamID) line inside the ply:InitInventory() function

Code:



AddCSLuaFile()


--Information
SWEP.PrintName="Inventory"
SWEP.Author=""
SWEP.Instructions="Right click on an item to add it to your inventory.
Left click to open your inventory."
SWEP.Category="Inventory"
--Spawn Info
SWEP.Spawnable=true
SWEP.AdminOnly=false
--Clip Info -1 on clips cause it has no ammo. I think Primary and Secondary is lmb and rmb?
SWEP.Primary.ClipSize=-1
SWEP.Primary.DefaultClip=-1
SWEP.Primary.Automatic=false --Don't want it to continually fire
SWEP.Primary.Ammo="none"
SWEP.Secondary.ClipSize=-1
SWEP.Secondary.DefaultClip=-1
SWEP.Secondary.Automatic=false --Don't want it to continually fire
SWEP.Secondary.Ammo="none"
--More Info
SWEP.Weight = 1 --Higher the weight the more likely you are to switch to it when current weapon runs out of ammo
SWEP.AutoSwitchTo=false
SWEP.AutoSwitchFrom=false
SWEP.Slot=1 --Which button the weapon will be under
SWEP.SlotPos=1 --Where under the button it will be
SWEP.DrawAmmo=false
SWEP.DrawCrosshair=true
--Models
SWEP.ViewModel=""
SWEP.WorldModel=""

invDirectory=""

local ply = FindMetaTable("Player")
ply.inventory={}

function ply:GetSavedInventory(steamID)
	
end

function ply:SetInvDirectory(steamID)
	playerID=string.Replace(steamID,":","-")
	invDirectory="inventory/"..playerID.."/invdata.txt"
end

function ply:GetInvDirectory()
	return invDirectory
end

function ply:InitInventory(size)
	if size<=0 then print("Inventory cannot be <= 0.") return nil
	else self.inventory.size=size end
	print("Size is: "..self.inventory.size..".")
	
	self:SetInvDirectory(self:SteamID())
	print("Inventory directory: "..invDirectory..".")
end
	
function SWEP:Initialize()
	self:SetHoldType("normal")
	
	ply:InitInventory(10) --Inventory can hold 10 different items
end

--Called when LMB clicked
function SWEP:PrimaryAttack()
	
end

--Called when RMB clicked
function SWEP:SecondaryAttack()
	
end


I’m sure I must be doing something wrong due to the fact that I don’t understand GMOD lua very well, so anything you can teach me would help greatly!

How I think this works is, weapon_inventory.lua is a shared file? And I’m creating 1 ply variable local to this file on both the server and client side but they’re separate variables in memory and not the same? So, both the client and server should get the variable set to the player’s data and call their own instances of the functions within the file.

I’ve tried just defining the ply functions and ply variable within an


if CLIENT then end

and then using an


if CLIENT then end

inside SWEP:Initialize() to call ply:InitInventory() and all the subsequent functions but the value was still null in ply. I’ve even done the same if statments but with SERVER instead of CLIENT and nothing. I’ve also tried setting ply to LocalPlayer() inside the CLIENT if then statement along with the ply functions with still no luck, and I’ve tried setting ply to an empty table and setting it with FindMetaTable() inside SWEP:Initialize(), which also didn’t work. I’m lost as to what I’m doing wrong, because, as far as I know, I thought this should work.

Any help would be greatly appreciated, thank you!