Attempt to index nil with 'Backpack'

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Just to fix it without error and make the script work.

  2. What is the issue? Include screenshots / videos if possible!
    Workspace.Trophy.ProximityPrompt2.Script:5: attempt to index nil with ‘Backpack’

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried to figure out how to get LocalPlayer and Backpack but it still says index nil with Backpack. So how do I make it possible to fix the error?

  4. What you want to do?
    I just want to remove a specific tool but it does not work
    The Problem on line 5: attempt to index nil with ‘Backpack’

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- 
local ToolName = {"Trophy"}  
local Storage = game.ServerStorage
local Part = script.Parent.Parent
local ProximityPrompt = script.Parent
local BP = game.Players.LocalPlayer.Backpack
local tool = BP:FindFirstChild(ToolName)

ProximityPrompt.Triggered:connect(function(Player)
	script.Parent.Parent.Transparency = 0
	script.Parent.Parent.CanCollide = true
	script.Parent.Enabled = false
	script.Parent.Parent.ProximityPrompt.Enabled = true
	tool:Destroy()
end)```

Please do not ask people to write entire scripts or design entire systems for you. If you can't answer the three questions above, you should probably pick a different category.

Is this in a server or local script?

1 Like

I believe this is because you’re referencing the Backpack before it exists when the player joins. Try doing the following instead:

local BP = game.Players.LocalPlayer:WaitForChild("Backpack")

Also, because this is running right when the player joins, I would be under the assumption that you probably won’t have any tools in the backpack (unless it’s in StarterPack of course.) If it’s not in StarterPack, instead you should refrence the tool in the ProximityPrompt.Triggered event, and then check if the tool exists.

1 Like

It’s on server script and of course it’s under ProximityPrompt

I’ve tried yours but this time the error is
'attempt to index nil with ‘WaitForChild’ ’

Oh. You can’t reference LocalPlayer in a ServerScript. Assuming you’re just looking for Trophy and nothing else, do this instead:


local Storage = game.ServerStorage
local Part = script.Parent.Parent
local ProximityPrompt = script.Parent

ProximityPrompt.Triggered:connect(function(Player)
	local BP = Player.Backpack

	if BP:FindFirstChild("Trophy") then
		BP:FindFirstChild("Trophy"):Destroy()
		script.Parent.Parent.Transparency = 0
		script.Parent.Parent.CanCollide = true
		script.Parent.Enabled = false
		script.Parent.Parent.ProximityPrompt.Enabled = true
	end
end)

Might also be worth it to check if it’s in the character as well. Also you have a few variables there you don’t seem to be using, you might want to try replacing your script.Parent.Parent's with Part.

1 Like

There’s no problem with it but something that I’m working on isn’t working(script do nothing).

Are you sure you have a Trophy in your Backpack? The script only works if it finds a Trophy in the Backpack.

1 Like

What do you want to happen exactly? All I’ve done is added a check to see if the tool exists in your backpack to prevent error, as well as changed the method you were getting the player from.

1 Like

Btw yea, when we interact with the trophy, it could make my trophy gone and the tool will be under the backpack right? after that, the part (which is this script) click again the trophy will appear. So it’s like a take and place system.

Btw yea, when we interact with the trophy, it could make my trophy gone and the tool will be under the backpack right? after that, the part (which is this script) click again the trophy will appear. So it’s like a take and place system.
It’s something like that

Ok thank you sorify but I edit it a little bit however, I got a new thing to achieve, hope you can go to the next topic to help me. see ya!