98 lines
2.6 KiB
Plaintext
98 lines
2.6 KiB
Plaintext
-- Services
|
|
local Players = game:GetService("Players")
|
|
local RunService = game:GetService("RunService")
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
local ServerScriptService = game:GetService("ServerScriptService")
|
|
|
|
local DataStoreNames = require("./DataStoreNames")
|
|
|
|
-- ProfileStore
|
|
local ProfileStore = require(ServerScriptService.Libraries.ProfileStore)
|
|
local ValueNames = require("./ValueNames")
|
|
|
|
local function GetStoreName()
|
|
return RunService:IsStudio() and "Test" or "Live"
|
|
end
|
|
|
|
local Template = require(ServerScriptService.Data.Template)
|
|
local DataManager = require(ServerScriptService.Data.DataManager)
|
|
|
|
-- Access profile store
|
|
local PlayerStore = ProfileStore.New(GetStoreName(), Template)
|
|
|
|
|
|
|
|
-- Add leaderstats and synchronize player data
|
|
local function Initialize(player: Player, profile : typeof(PlayerStore:StartSessionAsync()))
|
|
-- Leaderstats
|
|
local leaderstats = Instance.new("Folder", player)
|
|
leaderstats.Name = "leaderstats"
|
|
|
|
local Cash = Instance.new("NumberValue", leaderstats)
|
|
Cash.Name = ValueNames.Cash
|
|
local InitMoney = DataManager.GetValue(player, ValueNames.Cash) or 0
|
|
Cash.Value = InitMoney
|
|
|
|
DataManager.SyncValue(player,Cash,ValueNames.Cash,DataStoreNames.Cash)
|
|
--imma see if it works prob not lol
|
|
local Wins = Instance.new("NumberValue",leaderstats)
|
|
Wins.Name = ValueNames.Wins
|
|
DataManager.SyncValue(player,Wins,ValueNames.Wins,DataStoreNames.Wins)
|
|
|
|
end
|
|
|
|
|
|
-- Creates and stores a profile
|
|
local function PlayerAdded(player: Player)
|
|
|
|
-- Start a new profile session
|
|
local profile = PlayerStore:StartSessionAsync("Player_" .. player.UserId, {
|
|
Cancel = function()
|
|
return player.Parent ~= Players
|
|
end,
|
|
})
|
|
|
|
-- Sanity check to ensure profile exists
|
|
if profile ~= nil then
|
|
|
|
profile:AddUserId(player.UserId) -- GDPR compliance
|
|
profile:Reconcile() -- Fill in missing data variables from template
|
|
|
|
-- Handles session-locking
|
|
profile.OnSessionEnd:Connect(function()
|
|
DataManager.Profiles[player] = nil
|
|
player:Kick("Data error occured. Please rejoin.")
|
|
end)
|
|
|
|
-- Save profile for later use
|
|
if player.Parent == Players then
|
|
|
|
DataManager.Profiles[player] = profile
|
|
Initialize(player, profile)
|
|
|
|
else
|
|
profile:EndSession()
|
|
end
|
|
|
|
|
|
else
|
|
-- Server shuts down while player is joining
|
|
player:Kick("Data error occured. Please rejoin.")
|
|
end
|
|
|
|
end
|
|
|
|
|
|
-- Early joiners
|
|
for _, player in Players:GetPlayers() do
|
|
task.spawn(PlayerAdded, player)
|
|
end
|
|
Players.PlayerAdded:Connect(PlayerAdded)
|
|
|
|
Players.PlayerRemoving:Connect(function(player)
|
|
local profile = DataManager.Profiles[player]
|
|
if not profile then return end
|
|
profile:EndSession()
|
|
DataManager.Profiles[player] = nil
|
|
end)
|