Saving Multiple Tools of the Same Name

This script saves the players tools in a datastore, but if you pick up multiple of the same tool, you will only return with one.
How would I make this script save the amount of a certain tool you have, so for example, if I pick up 2 swords, I’ll rejoin with 2, not just 1.

local ToolFolder = game:GetService("ReplicatedStorage"):FindFirstChild("Items")
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)
1 Like

For anyone potentially wondering in the future, I just found this script that really helped me out:

--[[

Want an explanation of the script? ٩(。•́‿•̀。)۶
https://youtu.be/_bsiGhHErHw
	
--]]

local rs = game:GetService("ReplicatedStorage")
local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("saveStore")
local library = rs:WaitForChild("Items")

--< directory functions

local dir = {}

local function edit(player, list)
	dir[player.Name] = list
end

local function setup(player, list)
	for i = 1, #list do
		local tool = library:FindFirstChild(list[i])
		if tool then
			local clone = tool:Clone()
			clone.Parent = player.Backpack
		else
			print(list[i] .. " not found")
		end
	end
end

--< player events

game.Players.PlayerAdded:connect(function(player)

local ready = false

	player.CharacterAdded:connect(function(char)

	local bp = player.Backpack
	local data = nil
	
	if ready == false then
		ready = true
		
		data = store:GetAsync(player.UserId)
		
		if data then
			setup(player, data)
			edit(player, data)
		end
	end	
	
	char.Humanoid.Died:connect(function()
		char.Humanoid:UnequipTools()
		
		local old = player.StarterGear:GetChildren()
		for i = 1, #old do
			old[i]:Destroy()
		end
		
		local new = player.Backpack:GetChildren()
		for i = 1, #new do
			new[i].Parent = player.StarterGear
		end		
	end)	
	
	--< adjuster
	
	local count = 0
	
	local function adjust()
		
	if char.Humanoid.Health > 0 then
	
		local list = {}
	
		local equipped = char:FindFirstChildOfClass("Tool")
		if equipped then
			table.insert(list, equipped.Name)
		end	
	
		local tools = bp:GetChildren()
		for i = 1, #tools do
			table.insert(list, tools[i].Name)
		end
	
		if count ~= #list then
			edit(player, list)
			count = #list
		end
	end
	end
	
	--< child events
	
	bp.ChildAdded:connect(adjust)	
	bp.ChildRemoved:connect(adjust)	
	
	char.ChildAdded:connect(function(child)
		if child.ClassName == "Tool" then
			adjust()
		end
	end)
	
	char.ChildRemoved:connect(function(child)
		if child.ClassName == "Tool" then
			adjust()
		end
	end)	
	
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	store:SetAsync(player.UserId, dir[player.Name])
	dir[player.Name] = nil
end)

--< safety

game:BindToClose(function()
	wait(5)
end)

--[[ Shiro75 ]]--
2 Likes

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