Untitled
unknown
lua
4 years ago
3.2 kB
6
Indexable
--Manages the datastore for the game
--Make sure this is require()d at the beginning of the game
local Players = game:GetService("Players")
local ProfileService = require(script.Parent.ProfileService)
local ProfileStore = ProfileService.GetProfileStore(
"Player",
{
Money = 500,
Items = {},
IsBanned = false,
RetentionData = {},
NumberOfLogins = 0,
Wins = 0,
}
)
local Profiles = {}
local function onPlayerAdded(player)
local profile = ProfileStore:LoadProfileAsync(
"Player_" .. player.UserId,
"ForceLoad"
)
if profile ~= nil then
profile:AddUserId(player.UserId) -- GDPR compliance
profile:Reconcile() -- Fill in missing variables from ProfileTemplate (optional)
profile:ListenToRelease(function ()
Profiles[player] = nil
-- The profile could've been loaded on another Roblox server:
--player:Kick("Kicked due to session-lock: Are you playing on another server already? If not, please report this bug through the social links in the game description")
end)
if player:IsDescendantOf(Players) == true then
Profiles[player] = profile
--A profile has been successfully loaded:
profile.Data.NumberOfLogins = profile.Data.NumberOfLogins + 1
else
-- Player left before the profile loaded:
profile:Release() --Release session lock
end
else
-- The profile couldn't be loaded possibly due to other
-- Roblox servers trying to load this profile at the same time:
--player:Kick("Kicked due to session-lock: Are you playing on another server already? If not, please report this bug through the social links in the game description")
end
end
local function onPlayerRemoving(player)
local profile = Profiles[player]
if profile ~= nil then
profile:Release()
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
local DataManager = {}
--Main functions to interface with
function DataManager:Get(player) -- Warning: Can return nil for first few seconds when joining as a profile takes a short while to load
local profile = Profiles[player]
if profile ~= nil then
return profile.Data
else
warn("DataManager:Get was invoked before the profile of " ..player.Name .." has loaded (or they are not in the server)")
return nil
end
end
function DataManager:GetByUserId(playerId) -- Warning: Can return nil for first few seconds when joining as a profile takes a short while to load
local player = Players.GetPlayerByUserId(playerId)
return DataManager:Get(player)
end
function DataManager:GetWithMetaData(player) -- Warning: Can return nil for first few seconds when joining as a profile takes a short while to load
local profile = Profiles[player]
if profile ~= nil then
return profile
else
warn("DataManager:GetWithMetaData was invoked before the profile of " ..player.Name .." has loaded (or they are not in the server)")
return nil
end
end
-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(onPlayerAdded)(player)
end
return DataManager
Editor is loading...