Problem with player backpack

Okay so i have found a bug or maybe im just dumb but i have ALOT of problems with getting player’s backpack.

So for my game i need to detect when player equip/unequip a tool and when a tool is added to player backpack but it just doesn’t works…

here is my code :
image

and here is the output :
image

and i really don’t understand why on the second print backpack.Parent is nil since i take it from the player…

Edit1: “Katana” is a tool in the StarterPack

Byee

1 Like

Hello there, @fouroul94 . I am not quite sure what you are trying to achieve, but you said you need to detect when player equips or unequips the tool, you can do this by

player.Character.ChildAdded:Connect(function(child)
if child:IsA("Tool") and child.Name=="Katana" then
----do what you want to do here such as,
print(player.Name)
end
end)

Though I really want to know what you are trying to achieve here, if you can explain?

Also, if you are new to lua, I can help you with the entire code, the one I gave you is to help you understand.

2 Likes

actually i knew for this but i want to know once player join to detect tool in the BACKPACK
(im not new just first time using tools lol)

You mentioned equipped/unequipped. Its okay though,
If you want to detect when the tool is added to Backpack of the player, you can replace the player.Character.ChildAdded with backpack.ChildAdded
Though if you want to do this in a different way, I wouldn’t recommend you using while loop.
You can maybe use, repeat wait() until backpack:FindFirstChild("Katana")
As you said the tool is in the starterpack, there’s no doubt in it not being added to the player’s backpack.
Hope this helps.

nope it doesn’t work the problem i think is my line
backpack = player:WaitForChild(“Backpack”)
and in the output back.Parent is nil

also Backpack:GetChildren() return an empty table

Make sure you are using .PlayerAdded event, and that player is defined in the function,
if you can provide the whole code(if it isn’t too long) that would be better for me to help you.

So i found the issue its really easy.
Before tyring to get player backpack you have to make sure that his character is loaded so

player.CharacterAdded:Wait()
local backpack = player.Backpack

1 Like

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