Tables & ProfileService

I’m using an old script that I got from a tutorial when I was trying to learn how to use ProfileService, and whenever I try and add as a table a savable value, it returns as nil every single time. This is a massive issue as I’m trying to make a rarities game, and want to store some rarities through a table. Here is my code:

local ProfileTemplate = {
	Rolls = 0,
	Multi = 1,
	TimePlayed = 0,
	StoredRarities = {}
}

local ProfileService = require(game.ServerScriptService.ProfileService)

local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerData",
	ProfileTemplate
)

local Profiles = {} 

local function DoSomethingWithALoadedProfile(player, profile)
	local ls = player:WaitForChild("leaderstats")
	if ls then
		ls.Rolls.Value = profile.Data.Rolls
	end
	local sto = player:WaitForChild("stored")
	if sto then
		sto.Multi.Value = profile.Data.Multi
		sto.TimePlayed.Value = profile.Data.TimePlayed
		for i, v in ipairs(profile.StoredRarities) do
			if v then
				v.Parent = sto.Rarities
			end
		end
	end
end

local function PlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
	if profile ~= nil then
		profile:AddUserId(player.UserId) 
		profile:Reconcile() 
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick()
		end)
		if player:IsDescendantOf(Players) == true then
			Profiles[player] = profile
			DoSomethingWithALoadedProfile(player, profile)
		else
			profile:Release()
		end
		local DataLoaded = Instance.new("StringValue",player)
		DataLoaded.Name = "DataLoaded"
	else
		player:Kick() 
	end
end

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

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
	local profile = Profiles[player]
	profile.Data.Rolls = player.leaderstats.Rolls.Value
	profile.Data.Multi = player.stored.Multi.Value
	profile.Data.TimePlayed = player.stored.TimePlayed.Value
	for i, v in ipairs(player.stored.Rarities) do
		if v then
			table.insert(profile.StoredRarities,v)
		end
	end
	if profile ~= nil then
		profile:Release()
	end
end)

Here is the error:

2 Likes

Because you just added a new saving value, it can’t be in the loaded profile of the player yet. Try first checking if it exists in the loaded profile.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.