For the past week I’ve been trying to get a player’s backpack and move it into another players backpack. (move tools from one player to another)
I have two functions that run in the server, but it doesn’t seem to work. There are no errors in the output and I couldn’t find a solution on the forums. Does anyone have a different approach to this?
local function removeBackpackContent(player)
local backpackItems = {}
for _,v in pairs(player.character:GetDescendants()) do
if v:IsA("Tool") then
local itemClone = v:Clone()
table.insert(backpackItems,itemClone)
end
end
for _,v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
v:Destroy()
end
return backpackItems
end
local function addBackpackItems(player,backpackItems)
local playerBackpack = player:WaitForChild("Backpack")
for _,v in pairs(backpackItems) do
v.Parent = playerBackpack
end
end
I would think something like this would work better:
function giveitem(player, item)
item.Parent = player.Backpack
end
function getbackpackitems(player, target)
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
local clone = v:Clone()
v:Destroy()
giveitem(target, clone)
end
end
for i,v in pairs(player.Character:GetChildren()) do
if v:IsA("Tool") then
local clone = v:Clone()
v:Destroy()
giveitem(target, clone)
end
end
end)
local function getItems(player)
local tools = {}
local backpack = player.Backpack
for _,tool in ipairs(backpack:GetChildren()) do
if tool:IsA("Tool") then
table.insert(tools, tool)
end
end
local equipped = player.Character:FindFirstChildOfClass("Tool")
if equipped then
table.insert(tools, equipped)
end
return tools
end
local function parentTools(player, player2)
local tools = getItems(player)
for _,tool in ipairs(tools) do
local clone = tool:Clone()
clone.Parent = player2.Backpack
end
end
This will get all tools from the player, if equipped or not, and it will clone it into the second player’s backpack.