ProfileService Cloning Tool Into Player inventory

Hello developers,
I’ve been trying to use ProfileService to save players’ items when they leave, and then give them back when the player rejoins the game.
I first placed all the items in the player’s inventory into a table, then when the player rejoins, I clone the items from server storage into the player’s inv.

The problem is sometimes the server doesn’t clone the weapons into the player inventory, even though the server identifies the items inside the table. (printed table successfully)

Image reference:
Cloned successfully:

Cloned unsuccessfully:


(Prints the table’s items but doesn’t clone.)

Script:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")

local Template = require(ServerScriptService.PlayerData.Template)
local Manager = require(ServerScriptService.PlayerData.Manager)
local ProfileService = require(ServerScriptService.Libs.ProfileService)

local ProfileStore = ProfileService.GetProfileStore("Test4", Template)

local WeaponsFolder = ServerStorage.Weapons

local function LoadWeapons(player: Player)
	local profile = Manager.Profiles[player]
	if not profile then return end
	
	for _, Tools in pairs(profile.Data.Weapons) do
		if not WeaponsFolder:FindFirstChild(Tools.ToolName) then continue end
		local Tool = WeaponsFolder[Tools.ToolName]:Clone()
		Tool.Parent = player:WaitForChild("Backpack")
	end
	print(profile.Data.Weapons)
	table.clear(profile.Data.Weapons)
end

local function LoadProfile(player: Player)
	local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	if not profile then
		player:Kick()
		return
	end
	
	profile:AddUserId(player.UserId)
	profile:Reconcile()
	profile:ListenToRelease(function()
		Manager.Profiles[player] = nil
		player:Kick()
	end)
	
	if player:IsDescendantOf(Players) == true then
		Manager.Profiles[player] = profile
		LoadWeapons(player)
	else
		profile:Release()
	end
end

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

Players.PlayerAdded:Connect(LoadProfile)
Players.PlayerRemoving:Connect(function(player)
	local profile = Manager.Profiles[player]
	
	for _,tool in pairs (player.Backpack:GetChildren()) do
		if not tool:IsA("Tool") then continue end

		if WeaponsFolder:FindFirstChild(tool.Name) then
			table.insert(profile.Data.Weapons, {ToolName = tool.Name, Ammo = 44})
		end
	end
	
	if profile then
		profile:Release()
	end
end)
4 Likes

Maybe the profile loaded faster than the WeaponsFolder did?
Try printing WeaponsFolder:FindFirstChild(Tools.ToolName) and see what happens.

2 Likes


Prints successfully, this issue seems to be more frequent when the player has more items in the inventory.

2 Likes

Can you try print its FullName to see where it went?

for _, Tools in pairs(profile.Data.Weapons) do
    if not WeaponsFolder:FindFirstChild(Tools.ToolName) then continue end
    local Tool = WeaponsFolder[Tools.ToolName]:Clone()
    Tool.Parent = player:WaitForChild("Backpack")
    print(Tool:GetFullName())
end
1 Like


This is confusing, its saying the items are in my inventory but they arent. :thinking:

1 Like

Maybe u have folder or model that is named Backpack in player

then try using :FindFirstChildWhichIsA(“Backpack”)

or you are cloning folder/model instead of tool

1 Like

Does the explorer show the weapon inside your backpack?

Since it’s like that, can you try cloning on different amounts to test where the breaking point is

You can also paste the code in the command bar and execute it from the server during runtime to test if it works fine in a standalone context as well.

Nope, no children shown inside backpack.

image
Nope, I don’t have any other folders inside players except the default ones.

This seems unlikely but does the weapon has Archivable on? (If it didn’t it’d be weird how it cloned successfully in the first place, but it’s good to check anyways)

Yup, Archivable is set to true. I’ve also tested a few times, and it seems like the chance of :Clone() failing is very random, sometimes it usually fails when i have 5 items, but other times it takes a lot of tries when I have 7 items.

Anchold tool and test if there is anything in ur backpack

I’m gonna assume u meant anchore, nope sometimes it still fails to go into my inv.

This is a roblox issue, I created a similar script simulating yours and it didn’t work because Backpack probably couldn’t load right away. You have to add some yield to it.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Profiles = {}
local WeaponsFolder = ReplicatedStorage:WaitForChild("WeaponsFolder")

local function LoadWeapons(player: Player)
	local profile = Profiles[player]
	if not profile then return end

	for _, Tools in pairs(profile.Data.Weapons) do
		if not WeaponsFolder:FindFirstChild(Tools.ToolName) then continue end
		local Tool = WeaponsFolder[Tools.ToolName]:Clone()
		Tool.Parent = player:WaitForChild("Backpack")
	end
	print(profile.Data.Weapons)
	table.clear(profile.Data.Weapons)
end

local function GenerateProfile()
	local tab = {Data = {Weapons = {}}}
	for i = 1, math.random(2, 10) do
		table.insert(tab.Data.Weapons, {ToolName = "ClassicSword", Ammo = 44})
	end
	return tab
end

Players.PlayerAdded:Connect(function(player)
	Profiles[player] = GenerateProfile()
	-- If you do it instantly it doesn't work, try commenting the wait
	task.wait(5)
	LoadWeapons(player)
end)

repro.rbxl (53.5 KB)
If it still doesn’t work you can check my file, but it should work if you add some kind of yielding (task.wait(3)) right before the LoadWeapons function

Or use

player:CharacterAppearanceLoaded(Wait())

insted of task.wait

2 Likes

There’s also another issue of this code, if the player equip a sword, it is no longer inside the Backpack and thus, won’t be saved. You need to loop through the player as well (or :FindFirstChildWhichIsA("Tool").

This works, but it is this not that

player.CharacterAppearanceLoaded:Wait()

Thank you both for helping me find the issue! It probably won’t take me this fast to fix this if I hadn’t gotten any help. :pray: :pray: :pray:

Yep I knew that before, but I had to fix this major issue before I work on anything else :joy:

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