- Takes a
Playerobject - Returns
leaderstatsSession?
This initializes a player session. If settings.addLeaderstatsFolder is true, then it will also create a leaderstats folder under the player object with all public values exposed.
This method will return nil if the player is assumed to have left the game before they finished loading. If they are not a descendant of Players, then the function will yield for settings.loadingTimeout seconds or until they load in. In most cases this will not happen.
This will automatically clean up when the player leaves the server.
Example usage:
local greedyDataService = require(script.Parent.GreedyDataService)
local function playerAdded(player : Player)
greedyDataService:loadPlayer(player)
end
for _, player in playersService:GetPlayers() do
playerAdded(player)
end
playersService.PlayerAdded:Connect(playerAdded)- Takes a
Playerobject - Returns
leaderstatsSession?
This retrieves a leaderstatsSession if the player is in the game. If the player is no longer in the server, it will return nil.
- Takes a
Player,ValueName : string, andValue : any - Returns a
booleanstate value
Sets a data save value for the given player with the given index and value. If the value type does not match the existing value type, this will throw an error. Returns true if the data was successfully saved, and false if not.
The player must be in the server for this to work.
- Takes a
PlayerandValueName : string - Returns the data
valueandbooleanstate value
Gets a data save value for the given player with the given index. If the player does not exist, it will return nil, false. Otherwise, it returns value, true.
The player must be in the server for this to work.
greedyDataService:updateValueForPlayer(player : Player, valueName : string, transformer : (any?) -> any?) : any, boolean
- Takes a
PlayerandTransformer : function - Returns the
booleanstate value
Transforms a data save value for the given player with the given index and transformer function. If the player does not exist, it will return false. Otherwise, it returns true.
The player must be in the server for this to work.
- Takes a
UserId : number - Returns a
leaderstatsSession
Get a leaderstatsSession with a given user id. Does not require that the player is present in the server. Avoid calling this if the player is in the server.
- Takes a
valueName : string - Returns a
value
Call this to get the value of a leaderstatsSession.
Alternatively, you can index leaderstatsSession.data. So, for example, instead of doing:
local x = leaderstatsSession:get("myValue")You can do
local x = leaderstatsSession.data.myValue- Takes a
valueName : stringandvalue : any - Returns a
value
Sets a data save value with the given index and value parameters. If the value type does not match the existing value type, this will throw an error.
Alternatively, you can index leaderstatsSession.data. So, for example, instead of doing:
leaderstatsSession:set("myValue", 10)You can do
leaderstatsSession.data.myValue = 10Transforms a data save value with the given index and transformer function. The transformer function has an existing value in the parameter, and returns the updated value.
Alternatively, you can index leaderstatsSession.data. So, for example, instead of doing:
leaderstatsSession:update("myValue", function(oldValue)
return oldValue + 1
end)You can do
leaderstatsSession.data("myValue", function(oldValue)
return oldValue + 1
end)The dataTemplate is how you lay out the data to be saved.
Values can be public or private. If addLeaderstatsFolder is equal to true, then all public values are replicated in the player's leaderstats folder. Otherwise, all public values are made private in functionality.
This must return an array of values.
⚠️ DO NOT USE "[", "]", "{", OR "}" AT THE BEGINNING OR END OF A STRING VALUE! This will cause errors!
☝️ You may use strings, numbers, booleans, and tables as value types.
return {
public(0) "Money", -- Defaults to 0, named "Money". Will appear in leaderstats
public(0) "XP", -- Defaults to 0, named "XP". Will appear in leaderstats
private(0) "Joins", -- Defaults to 0, named "Joins". Will not appear in leaderstats
private("new") "Status" -- Defaults to "new", named "Status". Will not appear in leaderstats
}In this example, four datastore values are created, and of them "Money" and "XP" are declared public and thus replicated in the leaderstats folder.
Here is an example of a script using this structure:
local playersService = game:GetService("Players")
local greedyDataService = require(script.Parent.GreedyDataService)
local function playerAdded(player : Player)
local leaderstatsSession = greedyDataService:loadPlayer(player)
leaderstatsSession.data.Money = 1000 -- Sets "Money" to 1000
leaderstatsSession:set("XP", 500) -- Sets "XP" to 500
-- leaderstatsSession.data.whatever = value is the
-- same as leaderstatsSession:set("whatever", value)
-- For the rest of this, instead of using methods, I'll index .data
leaderstatsSession.data("Joins", function(lastNumberOfJoins)
return lastNumberOfJoins + 1
end) -- Increments "Joins" by 1
-- You can also do leaderstatsSession.data.Joins += 1
if leaderstatsSession.data.Status == "new" then
-- If a player is new, then say they are a rookie
leaderstatsSession.data.Status = "rookie"
elseif leaderstatsSession.data.Joins > 10 then
-- If they joined more than 10 times, they are a visitor!
leaderstatsSession.data.Status = "visitor"
elseif leaderstatsSession.data.Joins > 100 then
-- Wow, over 100 visits! They are now a regular.
leaderstatsSession.data.Status = "regular"
end
print(leaderstatsSession:get("Status"))
-- Prints "rookie", "visitor", or "regular"
print(leaderstatsSession.data.Status)
-- Should print the same thing again!
table.insert(leaderstatsSession.data.Inventory, `DailyReward#{leaderstatsSession.data.Joins}`) -- Add something to a table
print(leaderstatsSession.data.Inventory) -- Will get bigger every time you join!
end
for _, player in playersService:GetPlayers() do
playerAdded(player)
end
playersService.PlayerAdded:Connect(playerAdded)Declares a public value given a default value and value name. See the above example to see how this is used.
Declares a private value given a default value and value name. See the above example to see how this is used.