Add items to a table | ProfileService

Hello! So, I have this scripts that will give you a random trait. However, it returns this ServerScriptService.PlayerData.Manager:22: invalid argument #1 to ‘insert’ (table expected, got string)

Here’s the code:

Server | Leaderstats

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local Remotes = RS.Remotes
local SSS = game:GetService("ServerScriptService")

local Template = require(script.Parent.Template)
local ProfileService = require(SSS.Lib.ProfileService)
local Manager = require(script.Parent.Manager)

local ProfileStore = ProfileService.GetProfileStore("test", Template) -- test = testing purposes, Live for game publish

local function AddLeaderstats(player: Player)
	local profile = Manager.Profiles[player]
	
	if not profile then return end
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Inventory = Instance.new("Folder", player)
	Inventory.Name = "Inventory"
	
	
	local Rolls = Instance.new("NumberValue", leaderstats)
	Rolls.Name = "Rolls"
	Rolls.Value = profile.Data.Rolls
	
end

local function PlayerAdded(player: Player)
	local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	if profile == nil then
		player:Kick("Data Issue; Contact if issue persist!")
		return
	end
	
	profile:AddUserId(player.UserId)
	profile:Reconcile()
	profile:ListenToRelease(function()
		Manager.Profiles[player] = nil
		player:Kick("Data Issue; Contact if issue persist!")
		
	end)
	
	if player:IsDescendantOf(Players) == true then
		Manager.Profiles[player] = profile
		AddLeaderstats(player)
		
	else
		profile:Release()
	end
	
end

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

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player: Player)
	local profile = Manager.Profiles[player]
	if not profile then return end
	profile:Release()
end)

Template:

local module = {
	Inventory = {},
	Rolls = 0
}

return module

Adds Traits | ServerScript:

local RS = game:GetService("ReplicatedStorage")
local Remotes = RS.Remotes

local Manager = {}

Manager.Profiles = {}

function Manager.AdjustRolls(player: Player, amount: number)
	local profile = Manager.Profiles[player]
	
	if not profile then return end
	profile.Data.Rolls += amount
	player.leaderstats.Rolls.Value = profile.Data.Rolls
end

function Manager.AdjustInventory(player: Player, trait: StringValue)
	local profile = Manager.Profiles[player]

	if not profile then return end
	print(profile.Data.Inventory) -- returns a string value?
	table.insert(profile.Data.Inventory, trait)
	trait.Parent = player.Inventory
	--profile.Data.Inventory = player.Inventory
	
	print(profile.Data.Inventory)
	
end

return Manager

Hello, the error you’re encountering is because of a invalid argument being passed to the table.insert function. Try this,

function Manager.AdjustInventory(player, trait)
    local profile = Manager.Profiles[player]

    if not profile then return end
    if type(profile.Data.Inventory) ~= "table" then
        profile.Data.Inventory = {}
    end
    table.insert(profile.Data.Inventory, trait)
    trait.Parent = player.Inventory
end
1 Like

I get these errors

 Attempt to set parent of Players.Cxdious to Players.Cxdious.Inventory would result in circular reference
 Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters."