I’m currently trying to make a simple equip system. Apparently, the :GetChildren() method is not working. I have never encountered something like this before. When the function is ran, (which is when i press 3) It returns a nil table even though I can clearly see in the explorer tab that there are 3 tools in my player’s backpack.
elseif input.KeyCode == Enum.KeyCode.Three then
-- Equip Melee Weapon
print("pressed 3")
print(backpack:GetChildren())
for i, v in backpack:GetChildren() do
print("checking for itemslot")
if v:FindFirstChild("ItemSlot") then
if v.ItemSlot.Value == "Melee" then
player.Character.Humanoid:EquipTool(v)
end
end
end
end
Output:
This actually appears to be an issue with the game not detecting children of Backpack because previously I used a recursive :FindFirstChild() which also did not work, likely due to it detecting nil as a child of Backpack. Still do not understand why it is nil.
UPDATE
I found the problem which, makes absolutely no sense. Apparently defining backpack with a different variable breaks something, which again, makes absolutely no sense seeing as a variable is just a different way of stating something.
This does not work:
local player = game.Players.LocalPlayer
local backpack = player.Backpack
This does work:
local backpack = game.Players.LocalPlayer.Backpack
logically, it should work both ways seeing as player and game.Players.LocalPlayer are precisely the same thing, but who knows. If you are passing by and know why this -presumably a bug- occurs please let me know.
Unsure if you’re respawning beforehand, but something silly I found out is when the player respawns, the backpack is destroyed and replaced with a new one, then the tools are inserted into it.
This could be the problem if you’re defining the backpack in a variable at the top, since it would just store the very first backpack instance and never update when the player respawns.
When a player’s character spawns, the contents of the StarterPack and their StarterGear are copied into their Backpack. Once a character dies, the Backpack is removed and a new one will be created – populating it with the contents of StarterPack and StarterGear.
So you may be holding a reference to an old backpack. Try getting the backpack right before you need it like this:
print("pressed 3")
local backpack = player:FindFirstChildOfClass("Backpack")
print(backpack:GetChildren())
for i, v in backpack:GetChildren() do