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!

I want to make when the Gui name equals to item name that stored in replicated storage it will clone the item from replicated storage to player backpack

  1. What is the issue? Include screenshots / videos if possible!
    found an error in the line 14
    its says:
    ```Players.EducatedPilot04.PlayerGui.Inventory.IventoryPages.ScrollingFrame.Cube.LocalScript:14: attempt to index nil with ‘Backpack’``

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried to use bool value to check if item is equipped in player 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!

this is the code:

script.Parent.MouseButton1Click:Connect(function(player)
	print("ui clicked")
	
	local toys = game.ReplicatedStorage.Toys:GetChildren()
	print("getting item")

	for i , v in pairs(toys)do
		if script.Parent.Name == v.Name  and script.Parent.Equipped.Value == false then
			print("item loaded")
			
			local clone = v:Clone()
			v.Parent = player.Backpack
			script.Parent.Equipped.Value = true
			print("item success cloned :)))")
		end
	end
end)
-- This is an example Lua code block

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.

1 Like

MouseButton1Click returns nothing, so player is always going to be nil in your case. If this is a local script, just do local player = game:GetService("Players").LocalPlayer and remove the player from the brackets so it doesn’t overwrite

Its basically saying the player is nil.

  1. change script.Parent.MouseButton1Click:Connect(function(player)

to

script.Parent.Activated:Connect(function()

  1. Create a new varible, local player = game.Players.localplayer

  2. Make sure this is a local script

2 Likes
script.Parent.MouseButton1Click:Connect(function(player)
	print("ui clicked")
	
	local toys = game.ReplicatedStorage.Toys:GetChildren()
	print("getting item")

	for i , v in pairs(toys)do
		if script.Parent.Name == v.Name  and script.Parent.Equipped.Value == false then
			print("item loaded")
			
			local clone = v:Clone()
			v.Parent = player:WaitForChild("Backpack", 2)
			script.Parent.Equipped.Value = true
			print("item success cloned :)))")
		end
	end
end)
-- This is an example Lua code block

Use WaitForChild()

thnaks guys…it works fine now T_T

1 Like

The player is nil, won’t work.

Read the api

waiting literally won’t do anything

I forgot to add LocalPlayer :confused:

I know this is weird but may i know what do you mean by number 2 in this line

v.Parent = player:WaitForChild("Backpack", 2)

oh its number times out in that line

I See …thx(aaaaaaaaaaaaaaaaaaaaaaaaa)