Backpack is not a valid member of Player

I’m actually trying to make a system where if you rightclick on a player, he gets the currently equipped item.
The problem is, that I can’t get rid of this error: “Backpack is not a valid member of Player”

local player = game.Players.LocalPlayer
local Backpack = player.Backpack
local Tool = Backpack.Tool
local mouse = player:GetMouse()


Tool.Equipped:Connect(function() 
	
	mouse.Button2Down:Connect(function()
		
		local MouseTarget = mouse.Target
		if mouse.Target:FindFirstChild("Humanoid") then
		
			print(mouse.Parent)

end
end)
end)



4 Likes

Did you use this in LocalScripts
Local player only works in LocalScripts

2 Likes

Yep, StarterPlayer → StarterPlayerScripts

1 Like

Try to put a script in the tool instead

2 Likes

If you are using a LocalScript then, change

to

local Backpack = player:WaitForChild("Backpack")

2 Likes

I did, it still doesn’t work:

local player = game.Players.LocalPlayer
local Tool = script.Parent
local mouse = player:GetMouse()


Tool.Equipped:Connect(function() 
	
	mouse.Button2Down:Connect(function()
		
		local MouseTarget = mouse.Target
		if mouse.Target:FindFirstChild("Humanoid") then
		
			print(MouseTarget.Name)

end
end)
end)



1 Like

You didn’t add the line I told you to add.

2 Likes

I completely removed the Backpack line. The localscript is inside of the Tool now, so I can just do script.parent

1 Like

So what is your problem now? Show me your output.

2 Likes

I fixed it. Thank you though.

local player = game.Players.LocalPlayer
local Tool = script.Parent
local mouse = player:GetMouse()

local isEquipped = false

Tool.Equipped:Connect(function()
	isEquipped = true
end)

Tool.Unequipped:Connect(function()
	isEquipped = false
end)


mouse.Button2Down:Connect(function()
	if isEquipped == true then
		local MouseTarget = mouse.Target
if MouseTarget ~= nil then
			print(MouseTarget)
			end
		
	end
end)

3 Likes

Backpack might not be available immediately when the script runs. Using WaitForChild might have solved the issue, but it looks like you figured it out anyway.

1 Like

Use

local Backpack = player:WaitForChild("Backpack") or player.Backpack:Wait()

Edit : Oh, i didn’t notice that you fixed it already.

2 Likes