Feedback on "Backpack"

The 2nd code in the documentation of backpack which is:

local Players = game:GetService("Players")

local function giveTool(player, tool)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		tool.Parent = backpack
	end
end

local function onPlayerAdded(player:Player)
	local tool = Instance.new("Tool")
	giveTool(player, tool)
end

Players.PlayerAdded:Connect(onPlayerAdded)

will not work because the backpack resets every time the character is loaded, you can test it out yourself

Affected URL: Backpack | Documentation - Roblox Creator Hub

dont store the backpack in a variable it caused bugs for me just do tool.Parent = player.Backpack

It’s not my code it’s the code in the documentation of backpack

1 Like

Hello @YkKasper, thank you for flagging this! We are updating the code sample to the following:

local Players = game:GetService("Players")

local function onCharacterAdded(character)
  local player = Players:GetPlayerFromCharacter(character)
  local tool = Instance.new("Tool")
  local backpack = player:FindFirstChildOfClass("Backpack")
  tool.Parent = backpack
end

local function onPlayerAdded(player)
  if player.Character then
    onCharacterAdded(player.Character)
  end
  player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)
1 Like

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