Attempting to Index nil with 'Backpack'

I’ve been stuck on this piece of coding for a while now, im trying to detect if a tool’s parent is a players backpack

I’ve been trying to fix it myself but cant seem to figure it out.
script

The error
error

I’ve tried to look at other forums that I thought could of helped but still doesnt seem to fix my problem.

Thanks!, Shiva

This error has a simple fix really. :GetPlayerFromCharacter() can only be used if you have the players character, however, tools initially spawn in the player itself. Only when they are equipped are they then moved to the character. You’re code is working before you have the chance to even equip it. To fix this merely replace:

player = game.Players:GetPlayerFromCharacter(tool.Parent)

With this:

player = game:GetService("Players").LocalPlayer
1 Like

I feel like your code could be made much more efficient if you explain why you need to detect if the tools parent is the backpack.

If you want something to happen when the player puts away their tool (unequip) use tool.Unequipped.

If you want something to happen specifically when the tool is added to the player’s backpack (picked up and put away) use

tool:GetPropertyChangedSignal("Parent"):Connect(function()
    if tool.Parent:IsA("Backpack") then
        -- Do stuff here...
    end
end)

If you just want to know at any given point if the tool is in the backpack use

tool.Parent:IsA("Backpack")

It’s throwing that error because the player is nil. Add a check to make sure the player isn’t nil before checking for the backpack.

I feel really dumb now, I complete forgot about the :IsA(“Backpack”)!!!, sorry about not being more detailed, Im not the best at explaining : '(

the tool uses a motor6d as a grip, im trying to make it grip onto the right arm when it realizes its in a players backpack, so when a player equips the tool, its already welded onto the arm.

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