You need to create a new StatFunctions object, then call AddCash to it:
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) -- Create a new object
PlayerStats:AddCash(player, 10) -- Call the method from here
end)
Ok, I think I understand what’s happening, If you’re expecting the value of StatFunction.Cash to appear in the leaderboard after changing it, it won’t happen because of the way variables work.
You would have to try something like this:
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 -- Save a reference to the objects
self.Captures = leaderstats.Captures
self.Kills = leaderstats.Kills
self.Deaths = leaderstats.Deaths
return self
end
function StatFunctions:AddCash(player, amount)
self.Cash.Value += tonumber(amount) -- Add to the 'Value' property of the object
end
return StatFunctions
Now you should see changes appear on the leaderboard
local StatFunctions = {}
StatFunctions.__index = StatFunctions
StatFunctions.self = nil
function StatFunctions.new(player)
local self = {}
setmetatable(self, StatFunctions)
local leaderstats = player:WaitForChild("leaderstats")
self.Player = player
self.Cash = leaderstats.Cash -- Save a reference to the objects
self.Captures = leaderstats.Captures
self.Kills = leaderstats.Kills
self.Deaths = leaderstats.Deaths
StatFunctions.self = self
end
function StatFunctions:AddCash(player, amount)
StatFunctions.self.Cash.Value += tonumber(amount) -- Add to the 'Value' property of the object
end
return StatFunctions
This is definitely not the way to implement object oriented programming. Each call to the constructor will override the StatFunctions.self attribute and you don’t even get an object from the constructor.