I had previously asked this question on roblox forums and could not find any information so I direct my question to all you fellow developers out there, after spending roughly a year programming as a hobby I have come to the conclusion that I need better structure for how I layout my games. If anyone can direct me to some material with information as to different ways other developers have taken an approach to this. To be clear, I am not talking about DataStore, I am well aware how to use that.
A better example of how to show this is how I set up my own game structure keeping in mind that these are all folders containing different objects in each of them:
Workspace >Core >gameScripts >Script1 >Script2 >gameAssets >gunModel >remoteFunctions >remoteFunction1 >playerData >Trewier >Value1 >Value2
I'm not sure if there is a more efficient way as to how this all should be done, or if this should work as well as anything else out there. Any and all help is appreciated, thank you for your time.
-Trewier
Using folders is good. Some other possibilities compared to putting everything in the workspace:
In each of those services I put more folders to group related things (like "Scripts").
Example of ModuleScripts storing how much money someone has:
local module = {} local playerMoney = {} function InitializePlayerMoney(player) -- Initialize/load money playerMoney[player] = 0 end game.Players.PlayerAdded:Connect(InitializePlayerMoney) for _, player in ipairs(game.Players:GetPlayers()) do InitializePlayerMoney(player) end function module.GetPlayerMoney(player) return playerMoney[player] end function module.SetPlayerMoney(player, money) playerMoney[player] = money end game.Players.PlayerRemoving:Connect(function(player) -- Release memory; could also save money to datastore playerMoney[player] = nil end return module
Do note that since this module
Instead of saving a single number (money), you can modify this to save a dictionary of values, like playerData[player] = {Stats = {}, Money = 0, XP = 100}
. Then all your server scripts can access this information simply be requiring the ModuleScript -- the ModuleScript only initializes once and all the server scripts will share the data. (Clients will still need RemoteEvents/Functions to ask the server for this information, of course.)