How would i use OOP to make a Coin Collection system?

Hi! I’m currently learning OOP and so far i’ve learned how to make simple Powerups and Interactable objects with it. I want to make a Coin Collection system that uses leaderstats but doesn’t save any data, so no datastores, etc. I’ve tried to make one but when i touch the coin nothing happens. I’m not asking for you to make this for me, i’m just looking for some advice and also examples. Many thanks.

1 Like

A simple coin class with touch detection, right?

Here’s the general plan:

  • New coin class - clones a coin part and puts it into the workspace
  • Store a connection for when the coin is touched - when it is, check if it’s a player
  • If it’s a player, give the player cash and clean up the object
--ModuleScript


local players = game:GetService("Players")
local template = --[[path to a template you want to be cloned]]

local Coin = {}
Coin.__index = Coin

--class constructor
function Coin.new(position: CFrame, reward: number)
    local self = setmetatable({}, Coin) --initialise with metatable
    self.Coin = template:Clone() --clone a coin template
    self.Reward = reward --set the reward
    self.Coin.Parent = workspace --assign the parent
    self.Coin.CFrame = position --move to target position

    --detect when touched and see if it was a player. If it was, reward them
    self.Connection = self.Coin.Touched:Connect(function(hit: BasePart)
        local player = players:GetPlayerFromCharacter(hit:FindFirstAncestorOfClass("Model"))
        if not player then return nil end

        self.Connection:Disconenct()
        self:GiveCash(player)
        self:Cleanup()
    end)
end

--reward player
function Coin:GiveCash(player: Player)
    --give cash to leaderstat value
end

--cleanup a coin
function Coin:Cleanup()
    self.Connection:Disconnect() --in case you needed to cleanup from other function
    self.Coin:Destroy()
    table.clear(self)
end

return Coin

I hope this basic example helps! Please let me know if you have any questions or need me to explain anything.

2 Likes

Hey. Thanks, it actually works! I have a question tho. Is it better to just have the coins already exist in the game without needing to create them via the module? Or is it better to create locations for them and then create them and move them to these locations? And also, should i make a module for the leaderstats as well?

Glad it worked! In terms of having the coins already existing, you could do that, yes, but you’d need to modify the code a bit (the class constructor) to make it fit. Personally, I think it’s be easier to make the coins there and then without having them exist already, but it’s down to your choice.

If you want to make a module for your leaderstats, you can, but I wouldn’t recommend making it Object-Oriented. There’s a fine line between using OOP because it’s better for something and using OOP because it’s fun and cool and you want to use it for everything, and I’m not saying you’re doing that, but there are some cases where things are best left using functional-based programming instead of object-oriented programming.