I’ve just started using profileservice to organize player data, but I’m still new to it, and unsure of how it would be used. Here is the script im using, based off of the tutorial script in the devforum post. I see that the profile is loaded inside playeradded per player, but how can i get a specific player’s profile somewhere else, which can be saved the same way?
I tried loadprofileasync and then it gave me an error that the profile is already loaded,so i assume thats not the way to go.
local defaultData = {
Cash = 0,
Inventory = {},
Data = {},
}
----- Loaded Modules -----
local ProfileService = require(game.ServerScriptService.ProfileService)
local inventoryIndex = require(game.ServerScriptService.InventoryIndex)
local dataModule = require(game.ServerScriptService.DataModule)
----- Private Variables -----
local Players = game:GetService("Players")
local GameProfileStore = ProfileService.GetProfileStore(
"playerData",
defaultData
)
local Profiles = {}
local function inventoryOverrides(player, profile)
--Override items based on group role or something else specific
--Beta hat for Alacrware Studios Beta Testers and higher
if player:GetRankInGroup(7271083) >= 50 then
dataModule.appendIventoryItem(player, profile, "BETAHAT")
end
--Devhat for Alacriware Studios Developers and higher
if player:GetRankInGroup(7271083) >= 202 then
dataModule.appendIventoryItem(player, profile, "DEVHAT")
end
end
local function PlayerAdded(player)
local profile = GameProfileStore:LoadProfileAsync(
"Player_" .. player.UserId,
"ForceLoad"
)
if profile ~= nil then
profile:Reconcile() -- Fill in missing variables from defaultData
profile:ListenToRelease(function()
Profiles[player] = nil
-- The profile could've been loaded on another Roblox server:
player:Kick()
end)
if player:IsDescendantOf(Players) == true then
Profiles[player] = profile
-- A profile has been successfully loaded:
inventoryOverrides(player, profile)
else
-- Player left before the profile loaded:
profile:Release()
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()
end
end
----- Initialize -----
-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(PlayerAdded)(player)
end
----- Connections -----
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
local profile = Profiles[player]
if profile ~= nil then
profile:Release()
end
end)
--Services
local mps = game:GetService("MarketplaceService")
local GameProfileStore = ProfileService.GetProfileStore(
"playerData",
defaultData
)
--Remotes
local buyCashEvent = game.ReplicatedStorage.buyCash
--I want to make a function here that adds cash to the player's profile