Uses of OOP for storing players in a fps

I am currently developing a pvp fps game, and currently I am keeping track of players, their score, team, kills and other data linked to specific players in a dictionary.
I am then using a variety of functions to do things like damage the player, or change their gui according to game events. I am pretty new to roblox studio, and I am wondering if their are any uses for OOP in a situation like this were I am handling players on the server, or if just storing players in a dictionary is a fine system.
If anybody could just explain some common uses for OOP in a game like this I would be very thankful!

I’m not really sure, I am not using OOP when developing my FPS game.

In your case, you might be able to create a gun object or a weapon object and have them inherit from another object. Additionally, a player object you create might have methods to kill them or damage them.

Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and their interactions to design applications. In Roblox Studio, OOP can be useful for organizing and structuring your code.

One common use for OOP in a game like yours is to create classes for different types of objects in the game. For example, you could create a Player class that has properties such as score , team , and kills , and methods such as takeDamage() or updateGUI() . This allows you to encapsulate all the data and behavior related to a player in one object.

Here’s an example of what a simple Player class might look like:

local Player = {}
Player.__index = Player

function Player.new(name)
    local self = setmetatable({}, Player)
    self.name = name
    self.score = 0
    self.team = nil
    self.kills = 0
    return self
end

function Player:takeDamage(amount)
    -- code to reduce player health by amount
end

function Player:updateGUI()
    -- code to update player GUI based on game events
end

return Player

You can then create instances of the Player class for each player in the game:

local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
    players[player.Name] = Player.new(player.Name)
end

This allows you to easily access and manipulate data related to specific players using methods such as:

players["player1"]:takeDamage(10)
players["player2"]:updateGUI()

Overall, using OOP can help make your code more organized, reusable, and easier to maintain. However, it’s not strictly necessary - if you find that using a dictionary works well for your needs, then there’s no need to switch to an OOP approach.

2 Likes

Use OOP to create a custom player object, where you can store and modify their levels, check which guns they have unlocked, etc. You can use dictionaries, but using OOP makes the process cleaner.

PlayerData = {}
PlayerData.__index = PlayerData

function PlayerData.new(player)
    local playerData = {}

    playerData._player = player

    playerData._rank = 1
    playerData._experience = 0
    playerData._unlockedGuns = {}

    setmetatable(playerData, PlayerData)
    return playerData
end

function PlayerData:AddExperience(experience)
    self._experience += experience
    -- Calling PlayerData:AddExperience(experience) is cleaner than dictionary[playerID].experience += experience

    -- Not only that, but we can implement 'automatic' replication whenever a value changes
    if RunService:IsServer() then
        replicatePlayerData:FireClient(self._player, instructionID, experience)
    end    
end

function PlayerData:AddGun(gunID)
    -- Calling PlayerData:AddGun(gunID) is cleaner and more understandable than table.insert(dictionaries[playerID].unlockedGuns, gunID)
end

function PlayerData:Load(data)
    -- deserialise data here
end

function PlayerData:Serialise()
    -- return serialised data for saving data
end

return PlayerData

Personally, I don’t handle damage and GUI systems with OOP, because the former is already handled by the Humanoid and the latter can simply be a function in a client script (or client module script).

Thank you, this is very helpful to me!

Hello I have a few questions; just to let you know I do not know oop that well and I’m still trying to learn how it works. I was just searching the forum about how would I use oop in certain things and stumbled upon here, I’ve noticed something different from what I’ve seen.

What is the difference between the two? or there is absolutely no different about them and just functions the same? The former is taken from above and the latter is yours.

-- A
local self = setmetatable({}, Player)
return self

-- B
local playerData = {}
setmetatable(playerData, PlayerData)
return playerData

Concerning A, if you are declaring values in self after setting the metatable of the table, you will need to be careful to not activate any metamethods.

Concerning the names of variables, using self to refer to the new instance does not affect your program in any significant way.

Is a good idea storing the players inside the ModuleScript?