I have an inventory system where all inventories are stored in a table in a script on the server. At the moment I have a remote function for the client to get any inventory (their own) and that will fill up the gui.
However I want to be able to get this data from the server as well (to check if the player has certain items).
I already have two solutions for this but I wanted to get other people’s opinions on what is better / cleaner.
My ideas are:
Make my server side inventory script into a module script, but this would mean I have to make sure that it gets require()ed as soon as the game starts, so my code for setting up inventories when players join wont leave some players out (although I could just for Players:GetPlayers() do ... end and then Players.PlayerAdded).
Have a remote function and a bindable function, and give the same callback to each.
I’m just looking for an opinion and either one should work. Thanks for the help!
I would use a modulescript since they’re pretty much meant for this, as well as being much cleaner.
I don’t think that it would be very difficult to have like an :Init() function or something that is called when the game starts, and then have the other functions you need in there as well. B
Hey, just store Folders inside Player + you can set attributes to those Folders so you will be able to get these Items and their properties by running MainFolder:GetChildren().
As @BilgeRatJack stated, this is what ModuleScripts were meant for. In this way, they replace the utility of _G.
You can also store functions inside of the same ModuleScript to index the inventory tables, so that they live in a more relevant location, rather than performing those tasks inside the scripts that require the inventory module.
inventories = {}
module = {
DeleteInventory = function(player)
inventories[player] = nil
end,
CreateInventory = function(player,inventoryData),
if not inventories[player] then -- safety first!
inventories[player] = inventoryData or {} -- their inventory data, or a blank inventory template
end
end,
GetInventory = function(player)
return inventories[player]
end,
-- add more functions here to manipulate inventories, such as adding items to them, or clearing them
}
return module
-- server script
local inventoryModule = require(yourModule)
game.Players.PlayerAdded:connect(function(player)
local inventoryData = -- load their inventory data here
inventoryModule.CreateInventory(player,inventoryData)
end)
game.Players.PlayerRemoving:connect(function(player)
-- save their inventory data here
inventoryModule.DeleteInventory(player)
end)
^ this would be necessary only in the case where players are present before the scripts are loaded. If you wanted to do that in the example I provided above, you’d probably take the PlayerAdded and PlayerRemoving events and just bind them to the Load/Save data functions, which could be called in such a loop on startup
-- on startup LoadData connected to PlayerAdded
LoadData = function(player)
inventoryData = -- load their data here
inventoryModule.CreateInventory(player,inventoryData)
end
for _,player in next,game.Players:GetPlayers() -- load data for all players that might have been in the game before PlayerAdded was connected
LoadData(player)
end
game.Players.PlayerAdded:connect(LoadData)
… and you’d go on to do the same with PlayerRemoving