Help with Object Oriented Programming

Seconding @HugeCoolboy2007 .

Using WaitForChild on the leaderstats and saving the ValueBase instance rather than their values should work.

I am not sure I understand why you parameter the player when you have the self argument (which is your new instance object).

Would suggest this instead: (in addition, I personally prefer stating the type of the parameter, then not needing to convert it, but that is up to you)

function StatFunctions:AddCash(amount : number)
	self.Cash.Value += amount
end
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local StatFunctions = require(ServerStorage.StatFunctions)

Players.PlayerAdded:Connect(function(player)
    local playerStat = StatFunctions.new(player)
    playerStat:AddCash(10)
end)

it doesn’t find the leaderstats because it fires before the folder (what I’m assuming) spawns in so you should make a slight delay like around 2 seconds

That’s what :WaitForChild is for. It yields the expression until the folder spawns.

Thanks alot! Ill try out your advice when I can!

1 Like

I just tried your solution, but unfortunately that did not work.

No errors in the output.

I’m assuming its because the leaderstats is yielding infinitely.

Are the leaderstats ever being added to the player?

1 Like

Yes. The leaderstats are loading. The money isn’t adding to the player for some reason.

1 Like

What’s your current code right now that’s not erroring?

1 Like

My OOP Module Script:

local StatFunctions = {}

StatFunctions.__index = StatFunctions

function StatFunctions.new(player)
	local self = {}
	
	setmetatable(self, StatFunctions)
	
	local leaderstats = player:WaitForChild("leaderstats")
	
	self.Player = player
	self.Cash = leaderstats.Cash
	self.Captures = leaderstats.Captures
	self.Kills = leaderstats.Kills
	self.Deaths = leaderstats.Deaths
	
	
	
	
	return self
end

function StatFunctions:AddCash(amount : number)
	
	self.Cash.Value += amount
	
end


return StatFunctions

And how is it being called from the other script?

1 Like
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local StatFunctions = require(ServerStorage.StatFunctions)


Players.PlayerAdded:Connect(function(player)
	local PlayerStats = StatFunctions.new(player)
	StatFunctions:AddCash(10)
end)

Also can you add print(self) to the AddCash function and show me the result?

(If it just says table:0x847373 or whatever turn off log mode in the settings)

1 Like

It now errors:
ServerStorage.StatFunctions:26: attempt to index nil with 'Value'

So that makes me think that it’s failing to initiate the object.

function StatFunctions:AddCash(amount : number)
	print(self)
	self.Cash.Value += amount
	
end

Try this and show me the output.

The table is the print(self)

and the error is from

self.Cash.Value += amount

Replace your self variable with

local self = setmetatable({}, StatFunctions)

I think that’s the problem right now?

2 Likes

Pretty sure it’s adding the cash, I just don’t think its displaying it.

Doesnt look like anywhere in the script you are updating leaderstats.Cash, update it in the :AddCash function and see how that works. (set leaderstats.Cash.Value to self.Cash after you increment the self.Cash)

1 Like

You haven’t set Cash yet, you need to use your StatFunctions.new() to make cash a real value else it will return nil and possibly cause this error

1 Like

What if it waits for the leaderstats, but it’s children don’t exist yet? Try adding wait for child on the cash.

1 Like

Maybe try this. It seems like the leaderstats haven’t loaded when you call the constructor.

local StatFunctions = {}

StatFunctions.__index = StatFunctions

function StatFunctions.new(player)
	local self = {}
	
	setmetatable(self, StatFunctions)
	
	local leaderstats = player:WaitForChild("leaderstats")
	
	self.Player = player
	self.Cash = leaderstats:WaitForChild("Cash")
	self.Captures = leaderstats:WaitForChild("Captures")
	self.Kills = leaderstats:WaitForChild("Kills")
	self.Deaths = leaderstats:WaitForChild("Deaths")
	
	return self
end

function StatFunctions:AddCash(amount : number)
	
	self.Cash.Value += amount
	
end


return StatFunctions
1 Like