How to save IntValues inside of tools?

Right now, I have a script that saves tools in a player’s backpack that is also in a folder found in ServerStorage, but I also want to save the value of an IntValue inside of each tool, which could be unique for each tool. How would I go about doing this?

Current Script:

local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedTools")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(Player) 
	local ToolData = SaveData:GetAsync(Player.UserId)
	
	local Backpack = Player:WaitForChild("Backpack")
	local StarterGear = Player:WaitForChild("StarterGear")
	
	if ToolData ~= nil then
		for i,v in pairs(ToolData) do
			if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
				ToolFolder[v]:Clone().Parent = Backpack
				ToolFolder[v]:Clone().Parent = StarterGear
				
			end
		end
	end
	
	Player.CharacterRemoving:Connect(function(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local ToolTable = {}
	
	for i,v in pairs(Player.Backpack:GetChildren()) do
		table.insert(ToolTable, v.Name)
	end
	if ToolTable ~= nil then
		SaveData:SetAsync(Player.UserId, ToolTable)
	end
end)

Iterate over the backpack’s tools, fetch the NumberValue/IntValue instance from each tool, record the tool’s name and the NumberValue’s/IntValue’s value and record them in a DataStore using a dictionary, i.e;

--SAVING
local data = {}
for _, tool in ipairs(backpack:GetChildren()) do
	if tool:IsA("Tool") then
		local valueInstance = tool:FindFirstChildOfClass("NumberValue") or tool:FindFirstChildOfClass("IntValue")
		if valueInstance then
			data[tool.Name] = valueInstance.Value
		end
	end
end
dataStore:SetAsync(player.UserId, data)

--LOADING
local data = dataStore:GetAsync(player.UserId)
for toolName, value in pairs(data) do
	local tool = backpack:FindFirstChild(toolName)
	if tool then
		local valueInstance = tool:FindFirstChildOfClass("NumberValue") or tool:FindFirstChildOfClass("IntValue")
		if valueInstance then
			valueInstance.Value = value
		end
	end
end
1 Like

Decided to use fixed values, but thanks anyway.

1 Like