ProfileService how do I save tools from my players inventory and/or Character?

I want to be able to exit my game and re-enter it and have the same tool I left with, I also want all of my tools in a folder that my player owns in the future to be saved to as I will be making an inventory system soon.

Whole Idea: Have gun, leave game, enter game again, gun is saved.

DataManager Module Script:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local modules = ServerScriptService:WaitForChild("Modules")

local ProfileService = require(modules:WaitForChild("ProfileService"))
local Template = require(script:WaitForChild("Template"))
local leaderstats = require(script:WaitForChild("Leaderstats"))

local DataKey = "_DataStore"
local profileStore = ProfileService.GetProfileStore(DataKey, Template)

local datamanager = {}
datamanager.Profiles = {}

local function PlayerAdded(player: Player)
	local profile = profileStore:LoadProfileAsync("Player_"..player.UserId)
	
	if profile ~= nil then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		
		profile:ListenToRelease(function()
			datamanager.Profiles[player] = nil
			player:Kick()
		end)
		
		if player:IsDescendantOf(Players) then
			datamanager.Profiles[player] = profile
			leaderstats:Create(player, profile)
		else
			profile:Release()
		end
	else
		player:Kick()
	end
end

local function PlayerRemoving(player: Player)
	local profile = datamanager.Profiles[player]
	
	if profile ~= nil then
		profile:Release()
	end
end

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

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

return datamanager

Template Module Script:

local Template = {
	Neon = 0,
	Weapons = {},
}

return Template
1 Like

Simplest way to do this would be to store the names of the tools the player has. When they join (Players.PlayerAdded) loop through the tools recorded in the datastore and clone them into the players backpack.

There is some other considerations you will need to make, in the process of writing this functionality, such as how you will give players the correct tools again when they respawn.

Some resources to help you:

https://madstudioroblox.github.io/ProfileService/

2 Likes