Hi everyone! I’m new to using ProfileService and would like to know if I am using it correctly and efficiently. I have watched and read through multiple tutorials and read through the API. The DataStore Module is below and so are some questions I have.
My DataStore Module
--//Game Services\\--
local Players = game:GetService('Players')
--//Modules\\--
local ProfileService = require(game.ServerScriptService.Modules.ProfileService)
--//Profile Stuff\\--
local ProfileTemplate = {
TestValue = 0,
TestTable = {},
TestString = 'Test',
}
local TestDataStore = ProfileService.GetProfileStore("TestDataStore", ProfileTemplate) --//Get DataStore
local Profiles = {} --//Profile Table
--//PlayerDataManager\\--
local PlayerDataManager = {}
--//Global Functions\\--
function PlayerDataManager:AddValue(player, Value)
local Profile = Profiles[player] --//Get Player Profile.
if Profile then --//Profile Exists Then...
if typeof(Value) == 'number' then --//Check If Value Is Number.
Profile.Data.TestValue += Value --//Add Value.
print(Profile.Data.TestValue)
end
else
warn('Cannot Get Data From '..player.Name..'!!!') --//Warn Cannot Get Data From Player
end
end
--//Player Functions\\--
local function PlayerAdded(player)
local Profile = TestDataStore:LoadProfileAsync('Player_'..player.UserId) --//Load Player Profile.
if Profile ~= nil then
Profile:AddUserId(player.UserId)
Profile:Reconcile() --//Fill In Missing Variables.
Profile:ListenToRelease(function()
Profiles[player] = nil
--//If Loaded On Another Server, Kick Player.
player:Kick('Couldnt Load Your Data! Please Rejoin.')
end)
if player:IsDescendantOf(Players) == true then
Profiles[player] = Profile
--//Profile Was Loaded.
print(Profile.Data)
else
--//If Player Left Before Profile Was Loaded, Release Profile.
Profile:Release()
end
else
--//If Other Servers Try To Load Profile At Same Time, Kick.
player:Kick('Couldnt Load Your Data! Please Rejoin.')
end
end
local function PlayerRemoving(player)
local Profile = Profiles[player] --//Get Player Profile
if Profile ~= nil then --//If Profile Exists, Release It.
Profile:Release()
end
end
--//If Players Join Before Script Loads\\--
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerAdded, player)
end
--//Player Connections\\--
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)
game:BindToClose(function()
for i, player in pairs(Players:GetChildren()) do
PlayerRemoving(player)
end
end)
return PlayerDataManager
-
Is is necessary to use
:AddUserID()
? -
In the
:AddValue()
function I made, should I take more action then just a warn if the script cannot get data from the player? -
Is there anything more I should be adding to my
PlayerAdded()
andPlayerRemoving()
functions? -
Do I really need to loop through the players in case the script had not loaded in yet?
Part I'm Referring To
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerAdded, player)
end
- Do I need a
BindToClose(function()
when using ProfileService?
Thank you for the help in advance .