Help with profileservice

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

1 Like

Were did you put this script? ServerScriptService? You have to make a remote event in ReplicatedStorage. I don’t recommend going by tutorials on developer forum. I’m not a programmer but I know some stuff.

1 Like

It’s in serverscriptservice. The script itself works, but I havent been able to do much other than overrides because it can pass the profile on playeradded. I want to use the profile for a purchase, inside a remote function - but the remote function isn’t the problem.

ProfileService: Save your player data with ProfileService! (DataStore Module)

Could you explain what you mean? Do you mean how you could get the profile outside of that script?

Right now I have the inventoryOverrides function that runs from playerAdded (which is the tutorial script from the devforum post). I want to modify data to the profile in a separate function.

playeradded - loads profile
playeradded - passes profile to inventory overrides
inventory overrides - passes profile to append inventory item
append inventory item - modifies

but this originates from playeradded

You should be able to do that by modifying the data directly via requiring the module. You should only load the data once and in most cases only save the data once. You can get the data like this:

local ProfileCache = require(game.ServerScriptService.ProfileCacher)

-- where you want to change/use data
local playerProfile = ProfileCache[player]
if playerProfile ~= nil then
-- the pathway to your data might be different, it depends on how you've stored it in the first place
  playerProfile.Data.Inventory["SumValue"] -= 1

what is the profilecacher module?
or is that just profileservice?

Right. It’s just profile service. I named the module that connects to the profile service module directly ‘profilecacher’ as to not get confused with the main module.

Screenshot 2021-06-10 at 13.39.12

1 Like

I think I see where I’ve gone wrong. Do I need to write a module to cache profileservice data?

I think that’s the best way. You lose a lot of usability without the cache module, like it would be harder to connect the data to the rest of your game in an efficient way. Having said that most of what is in the module is what you might expect from a regular datastore script (in terms of loading/saving data anyway).

Still being somewhat new to profileservice and lua itself, if I make a module that has a table of profiles as each player loads (probably reusing a ton of the script above), will editing those profiles still save the data?

This is the bare-bones of what it should look like:

If you edit the player’s data directly as I suggested it won’t immediately save. It will only save properly when the player leaves and the profile is released along with the data inside of it. As long as that part is functioning any data you change during the session should save.

ok thank you very much for all the help!