Okay, a couple changes I would like to propose.
Client Folder:
Just remove it. There is no point in having a Module.get()
function on the client. It would be way easier to just do this, as the module already updates the player attributes:
player:GetAttributeChangedSignal(attributeName):Connect(function()
local value = player:GetAttribute(attributeName)
end)
Noob Compressor?
First of all, what a terrible name. Second, what does it even do? I get that it “compresses data”, but for what purpose? After editing the “EasyDatastore” module and removing the “noobCompressor” module, everything still works fine.
Seperate Autosave Script:
Why not just combine the autosave feature into the module? Also, I would recommend you use a RunService.Heartbeat
function instead of while true do
. This is more efficient and allows for other tasks to be ran in the module.
This leaves me with my last proposal:
Module Name:
Because it looks better, I would recommend changing the name to “EasyDatastore” or “EasyDatastore2”, with a capital E.
All of the above changes leave you with a standalone module. No children in sight!
I have gone ahead and made all of these changes myself.
-
I commented out any mentions of “noobCompressor” and adjusted the function to use the default data instead of the encoded/decoded/compressed data (I am not really sure what it did lol).
-
I removed any mention of the “get” event and altered the script to run on the server.
-
Combined the “autosave” script with the module at the bottom, changing the while true do
loop to a RunService.Heartbeat
function and calling any necesary functions. I then changed the module functions that deal with autosaving data to regular script functions, which will clean up the user experience when calling events with the module. I also adjusted the way that the the autosave function works. Before, the “autosave” script would only save a single player’s data every minute. This means that it would take 10 minutes to autosave everyone’s data once if there were 10 people in the game. I instead changed the function to loop through every player in the game and save everyone’s data every minute (or whatever the save frequency is set to).
-
Finally, I adjusted the name to have a capital E and placed it in ServerScriptService
, as it is far more secure there.
EDIT: Another thing I noticed with the documentation:
You shouldn’t require the module EVERY TIME a new player joins. You should instead require it once on the server and then manage it from there.
local EasyDatastore = require(ServerScriptService.EasyDatastore2)
Players.PlayerAdded:Connect(function(player)
local coins = EasyDatastore.set(player, "Coins", 10)
print(coins) -- 10
end)
EDIT-EDIT: Yet another thing:
The Module.subtract()
function does not check to ensure that the value will not result in a negative number.
What I would do is create a bool
parameter in the Module.subtract()
function called “canBeNegative” or something similar. If this is true, then don’t check if the value will result in a negative value, however, if it is false, check to make sure that the final updated value is not going to be negative.
function checkNegative(plr: Player, currencyName: string, amount: number): boolean
local currency = serverPlayersData[plr.UserId][currencyName]
if currency - amount < 0 then
return false
else
return true
end
end
function EasyDatastore.subtract(plr: Player, currencyName: string, amount: number, canBeNegative: boolean): number
local currency = EasyDatastore.get(plr, currencyName)
checkIsNum(currency, "subtract()", "currencyName")
checkIsNum(amount, "subtract()", "amount")
local negative = checkNegative(plr, currencyName, amount)
if canBeNegative or not negative then
return EasyDatastore.set(plr, currencyName, currency-amount)
else
return EasyDatastore.get(plr, currencyName)
end
end
EDIT-EDIT-EDIT: Types…
I noticed that with all of your functions you are not including types next to the parameters.
Doing this:
function Function(value: number, value2: string, value3: boolean): number
As opposed to this:
function Function(value, value2, value3)
Makes it a lot easier to understand what parameters to give functions, especially module functions.
Right now this is what the module shows when I call a function and pass in parameters:
If we add types to these parameters it suddenly becomes a lot more clear what should be passed in:
Here is the module with my applied edits:
EasyDatastore2.lua (13.3 KB)