ProfileService Trouble

So I finally moved to Profile Service to save Data in my game. But I’m trying to figure out how to add to the players value without breaking the Data or players Data or players Value, etc. Here is what I was testing:

local ProfileService = require(game.ServerScriptService.Leaderstats)

game.Players.PlayerAdded:Connect(function(player)
	local getPlayerData = ProfileService[player]
	while true do
		wait(5)
		getPlayerData.Data.Points += 100
	end
end)

And it gave me this error:

And line 7 is this: getPlayerData.Data.Points += 100

If you need, here is the Module script that is used to handle the leaderstats. (The ProfileService Module is in the Leaderstast Module)

local Players = game:GetService("Players")
local cachedProfiles = {}
local ProfileService = require(script.ProfileService)
local saveStructure = {
	Points = 0;
	Rubys = 0;
	Deaths = 0;
	Kills = 0;
	playTime = 0;
	Wins = 0;
}

local PlayerProfileService = ProfileService.GetProfileStore("PlayerLeaderstats", saveStructure)

local function playerDataLoaded(player)
	local profile = cachedProfiles[player]
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = profile.Data.Points
	points.Parent = folder
	
	local rubys = Instance.new("IntValue")
	rubys.Name = "Rubys"
	rubys.Value = profile.Data.Rubys
	rubys.Parent = folder
	
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Value = profile.Data.Deaths
	deaths.Parent = folder
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Value = profile.Data.Kills
	kills.Parent = folder
	
	local playtime = Instance.new("IntValue")
	playtime.Name = "playTime"
	playtime.Value = profile.Data.playTime
	playtime.Parent = folder
	
	local wins = Instance.new("IntValue")
	wins.Name = "WIns"
	wins.Value = profile.Data.Wins
	wins.Parent = folder
	
	local RoundKills = Instance.new("IntValue")
	RoundKills.Name = "RoundKills"
	RoundKills.Value = 0
	RoundKills.Parent = player
	
	spawn(function()
		while true do
			if profile ~= nil then
				points.Value = profile.Data.Points
				rubys.Value = profile.Data.Rubys
				deaths.Value = profile.Data.Deaths
				kills.Value = profile.Data.Kills
				playtime.Value = profile.Data.playTime
				wins.Value = profile.Data.Wins
			else
				break
			end
			wait(.1)
		end
	end)
	
	print(player.Name .. "'s data is loaded.")
end

local function PlayerAdded(player)
	local profile = PlayerProfileService:LoadProfileAsync("Player_" ..player.UserId, "ForceLoad")
	
	if profile ~= nil then
		profile:ListenToRelease(function()
			cachedProfiles[player] = nil
			player:Kick("Your data loaded Remotly, please rejoin.")
		end)
		
		if player:IsDescendantOf(Players) then
			cachedProfiles[player] = profile
			playerDataLoaded(player)
		else
			profile:Release()
		end
	else
		player:Kick("Data did not load, please rejoin or report in the links below of game Description.")
	end
end

for _, player in ipairs(Players:GetPlayers()) do
	spawn(function()
		PlayerAdded(player)
	end)
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
	local profile = cachedProfiles[player]
	if profile ~= nil then
		profile:Release()
	end
end)

return cachedProfiles

Let me know a modern version since I got this script from July of 2020.

Thanks for your help,

papahetfan

You might be getting the profile cache of the player too quickly, as those two PlayerAdded events run at the same time. What’s happening is that the first one tries to get the Profile while the other creates it, which results in this error.

You might need to wait until the player’s profile is created (I recommend creating a Signal).
This way, you can start updating their data as soon as it finishes being created.

The spawn() and wait() functions found here are also very outdated.

What I did for my game was to create an OOP system revolving around the player and their data, and call functions which fired an event, letting me update leaderstats when necessary.

If you need more help, feel free to message me directly.

You have a small misunderstanding of how ProfileService works. You’re doing everything correct except for getting the player’s profile from a separate script. The profiles aren’t stored inside of the ProfileService module directly but you’re trying to index the profile directly from the ProfileService module:

local getPlayerData = ProfileService[player]

Personally, I have a ModuleScript as a child of the data handler script called CachedProfiles and store the profiles there. Currently what you’re doing is creating a table inside of the data handler script called cachedProfiles and storing the profiles there, there’s no way for any other script to be able to access this table. Instead of doing that, do what I do and create a ModuleScript and store the profiles inside of there. Instead of trying to index the player’s profile from the ProfileService module, index the player’s profile from the CachedProfiles ModuleScript, which is where the profiles are stored.