Help with Object Oriented Programming

@tortoiseclips @Jumpathy @LightningLion58 @McThor2 @anthlons

Thanks for helping me! I’ll try out your solutions now and see if they work.

I really appreciate this :slight_smile:

2 Likes

Hopefully i helped you! and no worries

2 Likes

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.leaderstats = leaderstats
	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
	
	self.Cash.Value = self.leaderstats.Cash
	
	
	

end


return StatFunctions

You’re basically setting an Int to an Instance. Not sure what’s that supposed to do.

1 Like

Well I tried the script below and that didn’t work either.

local StatFunctions = {}

StatFunctions.__index = StatFunctions

function StatFunctions.new(player)
	local self = {}

	setmetatable(self, StatFunctions)

	local leaderstats = player:WaitForChild("leaderstats")

	self.Player = player
	self.leaderstats = leaderstats
	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)

	local leaderstats = self.leaderstats

	self.Cash.Value += amount

	self.Cash.Value = leaderstats.Cash.Value




end


return StatFunctions

I’m sorry if I’m frustrating you by the way. I’m trying my best to understand.

It’s not frustrating at all don’t worry. I was actually hoping I didn’t sound offensive/impatient :sweat_smile:/

Hold on, I’ll open the studio and try the script. Will edit this message accordingly.

(Also in my previous reply I meant you should delete that line, not define the leaderstats)

@ValiantWind using the following script worked for me:
image

local StatFunctions = {}

StatFunctions.__index = StatFunctions

function StatFunctions.new(player)
	local self = {}

	setmetatable(self, StatFunctions)

	local leaderstats = player:WaitForChild("leaderstats")

	self.Player = player
	self.leaderstats = leaderstats
	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
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)
Creating the leaderstats script
local function OnPlayerAdded(player)
	-- Setup leaderboard stats
	local leaderstats = Instance.new('Folder')
	leaderstats.Parent = player
	leaderstats.Name = 'leaderstats'

	local Captures = Instance.new('IntValue')
	Captures.Parent = leaderstats
	Captures.Name = 'Captures'
	Captures.Value = 0

	local Cash = Instance.new('NumberValue')
	Cash.Parent = leaderstats
	Cash.Name = 'Cash'
	Cash.Value = 0

	local Kills = Instance.new('IntValue')
	Kills.Parent = leaderstats
	Kills.Name = "Kills"
	Kills.Value = 0

	local Deaths = Instance.new('IntValue')
	Deaths.Parent = leaderstats
	Deaths.Name = "Deaths"
	Deaths.Value = 0
end

game:GetService("Players").PlayerAdded:Connect(OnPlayerAdded)

After a reread, the whole problem is you’re calling the stat functions on AddCash instead of PlayerStats

So do

PlayerStats:AddCash(…)

Instead of

StatFunctions:AddCash(…)

So the player is never defined or anything since you’re just referring to the module.

4 Likes

Here’s a short & simple mockup I made based on the cash part of your code, I’ll do my best to explain what’s happening throughout.

Server Script

local Players = game:GetService("Players")
local StatFunctions = require(script.StatFunctions) -- Requires the module (below)

local function CreateStats(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Parent = Player
	Leaderstats.Name = "leaderstats"
	
	local CashValue = Instance.new("NumberValue")
	CashValue.Parent = Leaderstats
	CashValue.Name = "Cash"
	CashValue.Value = nil -- Set this to whatever, if it's nil then it'll default to whatever is in the :AddCash() parenthesis below
	
	local NewStats = StatFunctions.new(Player) -- Create a new StatFunctions "Object"
	NewStats:AddCash(50)  -- Run the AddCash function in the module below, sending in the amount 50
end

Players.PlayerAdded:Connect(CreateStats)

Module (This is the child of the server script)

local StatFunctions = {}

function StatFunctions:AddCash(Amount)
	self.Cash += Amount -- Increment self.Cash by Amount - Remember, this value is originally what we set CashValue.Value to in the server script.
	self.Player.leaderstats.Cash.Value = self.Cash -- Update leaderstats as well, instead of just self.Cash
end

function StatFunctions.new(Player) -- Create the "Object"
	local self = setmetatable({}, {__index = StatFunctions})
	
    --[[
        The line below sets self.Cash to the existing value of leaderstats.Cash,
        Furthermore,  it's important that we make the stats *before* we call Stats.new() in the server script
        to ensure there is something to set self.Cash to!
    ]]
	self.Cash = Player.leaderstats.Cash.Value
	self.Player = Player -- Whos values will we be changing
	
	return self
end

return StatFunctions

Hopefully this can help you in understanding your code a little bit more! Let me know if you have any questions, or need any help.

3 Likes