Why are my OOP functions are nil?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am attempting to make my OOP data module.

  1. What is the issue? Include screenshots / videos if possible!

I get the error that LoadStats function is nil.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I cant find many good resources on OOP modules and no discord servers found anything.

Module

local StatsManager  = {}
StatsManager._index = StatsManager

local Settings = {}

local TableUtil = require(game.ServerScriptService.TableUtil)

Settings.TriesAllowed = 3
Settings.DataKey = "BetaKey"
Settings.StudioSave = true
Settings.DefaultTable = {Cash = 0}

local MainStore = game:GetService("DataStoreService"):GetDataStore(Settings.DataKey)

local PlayerObjects = {}

function StatsManager.new(player)
	local self = {}
	setmetatable(self, StatsManager)
	
	self.Key = "PlayerData-"..player.UserId
	self.Id = player.UserId
	self.Player = player
	
	PlayerObjects[player.Name] = self
	
	return self
end

function StatsManager:GetManager(player)
	return PlayerObjects[player.Name]
end

function StatsManager:LoadStats()
	
	local tries = 0
	local success
	
	local data 
	
	repeat
		tries = tries + 1
		
		success = pcall(function()
			data = MainStore:GetAsync(self.Key)
		end)
	until tries == 3 or success
	
	if data then StatsManager[self.Id] = data else StatsManager[self.Id] = TableUtil.deepCopy(Settings.DefaultTable) end
	
	local leaderstats = Instance.new("Folder", self.Player)
	leaderstats.Name = "leaderstats"
	
	for key,value in pairs(StatsManager[self.Id]) do
		local newValue = Instance.new("StringValue", leaderstats)
		newValue.Name = key
		newValue.Value = value
	end
end

function StatsManager:SaveStats()
	local tries = 0
	local success
	
	local data 
	
	repeat
		tries = tries + 1
		
		success = pcall(function()
			MainStore:SetAsync(self.Key, StatsManager[self.Id])
		end)
	until tries == 3 or success
end

function StatsManager:IncrementStat(statName, incrementValue)
	StatsManager[self.Id][statName] = StatsManager[self.Id][statName] + incrementValue
end

function StatsManager:SetStat(statName, newValue)
	StatsManager[self.Id][statName] = newValue
end

function StatsManager:GetStat(statName)
	return StatsManager[self.Id][statName]
end

function StatsManager:ClearStats()
	StatsManager[self.Id] = TableUtil.deepCopy(Settings.DefaultTable)
end

game.Players.PlayerRemoving:Connect(function(player)
	if PlayerObjects[player.Name] then
		PlayerObjects[player.Name] = nil
	end
end)

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() == false then
		for _,player in pairs(game.Players:GetPlayers()) do
			local PlayerStatManager = StatsManager:GetManager(player)
			PlayerStatManager:SaveStats()
		end
	end
end)

return StatsManager

Script

local StatsManager = require(script.Parent.StatsManager)

game.Players.PlayerAdded:Connect(function(player)
	local PlayerStatManager = StatsManager.new(player)
	PlayerStatManager:LoadStats()
end)

Two underscores.
StatsManager.__index

Still Nothing. The editor also states a single underscore is the correct way to to do it.

wait nvm the editor didnt

30 chgarssadasd

i am having the same issue as yours for now i replaced it with some stuff i read everything about oop and found nothing on how to set it

wait yes it did work, Thanks :slight_smile: