Table nills outside .new function

I have a player group module that stores players. When verifying additions to the player group, it errors out with the self.Verification turning into nil outside the .new function

playergroup module snippet:

local PlayerGroup = {}
PlayerGroup.__index = PlayerGroup

local verbose = true
local function log(...)
	if verbose then
		print(...)
	end
end

function PlayerGroup.new(name: string, defaults: {number}): PlayerGroup
	local self = setmetatable({}, PlayerGroup)
	self.Players = {}
	self.Name = name
	self.Verification = defaults
	
	log("New Player Group Created: "..name)
	return self
end

function PlayerGroup:Find(Player: Player): number
	return table.find(self.Players, Player)
end

function PlayerGroup:Verify(Player: Player): boolean -- edit: removed swear word from the snippet
	for _, id in ipairs(self.Verification) do
		if Player.UserId == id then
			return true
		end
	end
	return false
end

function PlayerGroup:Add(Player: Player, Verify: boolean): number
	if table.find(self.Players, Player) then
		log("Error: Player found when adding to group.")
		return 1
	end
	if Verify then
		if not PlayerGroup:Verify(Player) then
			return 1
		end
	end
	
	log("Player added to playergroup.")
	table.insert(self.Players, Player)
	return 0
end

return PlayerGroup

manager snippet:

local defaults = table.freeze({ -- guaranteed defaults even if profile doesn't load
	VIPAdmins = {
		2,
	},
})
local VIPAdmins = PlayerGroups.new("VIPAdmins", defaults.VIPAdmins)

Players.PlayerAdded:Connect(function(player: Player)
	VIPAdmins:Add(player, true)
end)
1 Like

Inside of the Add() function there’s a small error. PlayerGroup:Verify(Player) should be self:Verify(Player) otherwise it cannot access self.Verification inside the Verify() function.

2 Likes

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