Hey everyone! I’ve run into a problem and I’m hoping you can lend some input. I’ve constructed a module that keeps track of players and stores them in one of three tables: Lobby, when they join the game; Team1 when the round starts and players are chosen for Team1, and the same for Team2. For example, when a player joins the game, this code activates:
In a server script:
local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(Player) local PlayerManager = require(game:GetService("ServerScriptService").Modules:WaitForChild("PlayerManager")) PlayerManager.Insert("Lobby", Player) end)
In the module script:
function PlayerManager.Insert(tableName, Player) if tableName == "Lobby" then table.insert(Lobby, Player) end --etc for the other tables. end
This code works just fine how it’s written, and it stores everything how it should. However, with all the other functions and variables involved that I didn’t include in this snippet of code, the code gets unnecessarily long and complicated. I would like the module to accomplish this…
function PlayerManager.Insert(tableName, Player) table.insert(tableName, Player) end
…as this would cut down a lot of the code and would make it more efficient. I would essentially be passing the name of the table through the module function’s argument so that the name of the table can be used wherever it needs to be within the function. But alas, running it this way gives me a bad argument error (table expected - got string).
Am I approaching this from the wrong angle or is there a way this could be accomplished? Any help would be appreciated!