PlayerDataService
A module to manage the loading and unloading of player data. Built With ProfileService
Setting Up
-
To get started you must first edit
SETTINGS.SaveStructure
inPlayerDataService
. Add and remove variables ofSaveStructure
as you see fit your needs.Ran into an issue?, Troubleshooting
-
It's VERY VERY important that you understand
ProfileService
. Read the documentation and follow the creator onTwitter
😎
Testing
For testing using live profiles in a non destructive manner set
SETTINGS.MockProfiles
in SETTINGS.MockProfiles
to true.
Warning
GlobalUpdates do not run while profiles are being mocked. You should structure your game in a way that your testing place is separate from your main game to allow you to test without limitations.
Basic Usage
--!strict
--[[
{C.G.T}
-[TestService]---------------------------------------
A module for testing other modules
Members:
Functions:
Members [ClassName]:
Methods [ClassName]:
--]]
local SETTINGS = {}
----- Loaded Modules -----
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Knit = require(ReplicatedStorage.Packages.knit)
----- Module Table -----
local TestService = Knit.CreateService({ Name = "TemplateService" })
----- Private Variables -----
----- Private functions -----
----- Public -----
----- Initialize & Connections -----
function TestService:KnitStart()
local player_data_service = Knit.GetService("PlayerDataService")
local player = Players:GetPlayers()[1] or Players.PlayerAdded:Wait()
player_data_service:GetDataReplica(player):andThen(function(DataReplica)
DataReplica:SetValue("SomeData", DataReplica.Data.SomeData + 100)-- The value you're changing has to have been added in your save structure
end)
end
return TestService
--!strict
--[[
{C.G.T}
-[TestController]---------------------------------------
A module for testing other modules
Members:
Functions:
Members [ClassName]:
Methods [ClassName]:
--]]
local SETTINGS = {}
----- Loaded Modules -----
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Knit = require(ReplicatedStorage.Packages.knit)
local ReplicaController = require(game.ReplicatedStorage.Packages.replicaservice)
----- Module Table -----
local TestController = Knit.CreateController({ Name = "TestController" })
----- Private Variables -----
----- Private functions -----
----- Public -----
----- Initialize & Connections -----
function TestController:KnitStart()
ReplicaController.ReplicaOfClassCreated("PlayerData", function(replica)
if replica.Tags.Player == Players.LocalPlayer then
replica:ListenToChange("SomeData", function()
print("SomeData Changed")
--Do Something when SomeData changes
end)
end
end)
end
return TestController
Members
Functions
PlayerDataService:GetData(player):
PlayerDataService:GetProfile(player):
PlayerDataService:GetDataReplica(player):
PlayerDataService:GetDataReplica(player):andThen(function(profile_replica)
profile_replica:SetValue("SomeData", ProfileReplica.Data.SomeData + 100)
end)
PlayerDataService:AddGlobalUpdate(UpdateData)
To set up a GlobalUpdate
you should:
-
First make a GlobalUpdateHandler module in the GlobalUpdateHandler Folder(Member of PlayerDataService) and give it a unique name(Name should match type when adding one).
-
Make a an UpdateData Type at the top of the script for intellisense
-
return a function that takes
lua UpdateId: number
andlua UpdateData: UpdateData
as parameter to the function -
If false is returned the update won't be cleared and this same function will run on then next time they rejoin.
-
Lastly you just add a
GlobalUpdate
using the built in AddGlobalUpdate function.