tl:dr – I’m looking for best methods for handling players with data in small game.
Greetings Robloxians, this is my first post on Dev Forum, so let’s cut to the chase: I’ve written (A very sloppy, admittedly) way to handle players in a new game I’m working on by myself. The game is “Functional”. The thing it doesn’t do yet is handle players leaving the game while playing a game of Connect 4. The lobby/game is all in one place since it’s so simple and can support 4 games simultaneously all run with a single script. I’m unhappy with the result of handling players the way I am and think it would be best to redo the player handling altogether.
I wish to achieve this with OOP. I’m less looking for a script that does things for me, I just want to know I’m approaching this the best way. I’m thinking of having a table of “players” and inside would be sub-tables named after each player who joins. Each sub-table would have another sub-table or “property” in them for everything I wish to keep track of in the game.
ex:
Players = {}
Players[“ShadowOfCrimson”] = {
[“Board”] = Board1 – This would be one of the 4 potential board names used. Potential for double usage in that if this is nil that it means the player is not playing a game currently.
[“Wins”] = 42 – This is not tracked currently, but would be nice for future implementation of Data Persistence.
}
–When someone leaves, find a player’s partner by checking all players for having a matching board
for i,v in pairs(players) do
if v.Board == players.ShadowOfCrimson.Board then
–The partner is v
What I want to know is: Is this the best approach to handling this? What ways would you guys do this better? Anything is helpful as I’m just getting into using Lua’s OOP stuff and have been looking at online references to learn as much as I can. Thanks in advance for your time and effort!
how i recommend you approach your problem is by having one large table on the server that stores all the data in a module script like this:
--modulescript
local clientDataTable = {} -- the table in which all the data would be stored
local dataHandler = {}
return dataHandler
--then now you can begin to add in functions to manipulate the data
-- for example a function to add a player data to the table when they join
function dataHandler:newClientData(player,dataTbl)
if dataTbl then
--this here will check if the player has data already
clientDataTable[player.UserId] = dataTbl
else
--if the client does not have data then you can create a starter table for each player
clientDataTable[player.UserId] = self.getDefaultTable()
endr
end
function dataHandler.getDefaultTable()
--this function returns a starter table for a new player
return {
["Wins"] = 0
["Money"] = 100
}--just a example
end
--your gonna also wanna have a function to remove the players data from the server table and store it so you can do it like this
function dataHandler:removeClientData(player)
local data = clientDataTable[player.UserId]
clientDataTable[player.UserId] = nil --removing from server data table
self.saveClientData(player,data)
end
function dataHandler.saveClientData(player,data)
--here you would handling saving the data
end
--[[
you can add more functions to suit your needs for example you can
add a function call getPlayerDataFromIndex() that accepts the player
and index as parameters and returns the data.
]]--
--now all you have to do is attach these functions to the playerAdded and playerRemoving events in a script
--script
local datastore =--put data store here
local dataHandler = require(--[[module script here]])
game.Players.PlayerAdded:Connect(function(player)
local data = --get the data here
dataHandler:newClientData(player,data)
end)
game.Players.PlayerRemoving:Connect(function(player)
dataHandler:removeClientData(player)
end)
also, i would recommend you looking into using the Datastore 2 module that ensures that data is not lost. Also for your matchmaking system you could also use a table in a module script to store all relevant information and add your own functions to suit your needs
@RomeoEDD, I have looked at the DataStore2 model, I feel I must explain I am not trying to do data persistence as of this time, but in the future it is an option. I wish to handle players and keeping track of who is playing and who is not. I also want to know which players are at which boards and who is their partner at said board. The reason for doing this is to handle the event of someone leaving unexpectedly mid-game.