You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Im wondering how to make Global LeaderBoard(ordered data store) using profileStore
-
What is the issue? i am not very familiar with data stores(and profile store)
-
What solutions have you tried so far? i tried to make that when player leaves,data from profile store would be saving nto ordered data store but it failed
--SErvices
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ServerScriptService=game:GetService("ServerScriptService")
--ProfileStore
local ProfileStore = require(ServerScriptService.Libraries.ProfileStore)
local function GetStoreName()
return RunService:IsStudio() and "Test" or "Live"
end
local Template = require(ServerScriptService.Data.Template)
local DataManager = require(ServerScriptService.Data.DataManager)
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 Wins = Instance.new("NumberValue",leaderstats)
Wins.Name = "Wins"
Wins.Value = profile.Data.Wins
local Losses = Instance.new("NumberValue",leaderstats)
Losses.Name = "Losses "
Losses.Value = profile.Data.Losses
local Coins = Instance.new("NumberValue",leaderstats)
Coins.Name = "Coins "
Coins.Value = profile.Data.Coins
--sync player data
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 a profile exists
if profile ~= nil then
profile:AddUserId(player.UserId) --respect if player want to remove its data
profile:Reconcile() -- fill in missing data variables
--if players join on another server
profile.OnSessionEnd:Connect(function()
DataManager.Profiles[player] = nil
player:Kick("Data error.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 joining
player:Kick("Data error.Please rejoin.")
end
end
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)
my template is:
return {
Wins = 0,
Losses = 0,
Coins = 0
}
I want to save “WIns” to Ordered Data Store