Roblox Guide Help

PlayerData Module

In your wally.toml file, add this to your dependencies:

[dependencies] playerdata-module = "release/playerdata-module@1.3.9"

You have two solutions to import this module, depending if you wish to import the Controller or the Service on your own, or if you let the module give you the right code.

local PlayerDataModule = require(ReplicatedStorage.Packages["playerdata-module"])
local PlayerDataService = Knit.GetService("PlayerDataService")

Configuration

A file called DataModule.config.lua is to be created in ReplicatedStorage/Shared/Configs.

return table.freeze({ ProfilesTemplate = { ["Money1"] = 0, ["Money2"] = 0, ["Inventory"] = {}, }, Infos = { Key = "PlayerData", Versions = { Default = "1.0.0", Studio = "1.0.0", }, UseDefaultKey = false, -- Overrides the studio key even in studio. }, AutoLoad = true, BindToCloseDisabledInStudio = true })

Explanation:

  • ProfilesTemplate: A template for the ProfileService, it serves as a config for data saving.

  • Infos: Holds different information regarding the data store:

  • Key: The identifier to store data

  • Versions: Current version of the data key (can be used, as example, to wipe data).

  • UseDefaultKey: Allows to use the proper game key even in the studio.

  • AutoLoad: A boolean that, if set to true, allows the module to handle by itself data save and load. Set it to false if you want to use your own data system.

  • BindToCloseDisabledInStudio: A boolean allowing the game to save data in studio when it’s about to shut down completely.

Usage

This module provides functions to handle data saving, loading, and editing quite easily.

Events

  • OnDataLoaded: Triggers when the player's data is loaded.

  • OnDataUpdated: Triggers when the player's data is updated.

Methods (Server Only)

This method is used to load the player's data whenever they join.

Usage

Explanation

  • player: The player to load the data of.


game.Players.PlayerAdded:Connect(function(player) PlayerDataService:LoadProfile(player: Player): {} end)

    This method is used to save the player's data.

    Usage

    Explanation

    • player: The player to save the data of.


    game.Players.PlayerRemoved:Connect(function(player) PlayerDataService:SaveProfile(player) -- Careful ! A save profile should always be followed by a RemoveProfile PlayerDataService:RemoveProfile(player) end)

      These methods are used to free the player's data safely.

      Usage

      Explanation

      • player: The player to free the data of.


      game.Players.PlayerRemoved:Connect(function(player) PlayerDataService:SaveProfile(player) -- to save data before free-ing the profile. PlayerDataService:RemoveProfile(player) end)

      Explanation

      • callback: The callback to be called whenever PlayerRemoving is fired.


      PlayerDataService:ConnectToPlayerRemoved(function(player: Player) print("Player "..player.Name.." has been removed !") end)

        Usage

        Explanation

        • player : The player to load the data for.

        PlayerDataService:GetProfile(RandomPlayerInstance)

        PlayerDataService:YieldProfile(RandomPlayerInstance) --[[ Yield profile will wait for the profile to be returned in the event of you using the service before it's started. Overall a better choice for fetching profiles. ]]--

          If needed, you can fetch specific data from the player's store


          Explanation

          • player: the player you attempt to fetch / set data of

          • key: the key of the data you're wishing to fetch / set

          • value: the value of the data you're wishing to set


          PlayerDataService:GetValue(player: Player, key: string): any

          PlayerDataService:SetValue(player: Player, key: string, value: any)
          Last modified: 17 December 2024