How to access player backpack?

The button (GUI) script doesn’t work when I try to define a player backpack. Look:
(Server script)

local button = script.Parent
local SS = game:GetService("ServerStorage")

button.MouseButton1Click:Connect(function(buy)
	local backpack = game.Players.LocalPlayer.Backpack
	if backpack then
		print("Found player backpack")
	end
end)

and then the output is:

Players.UrAnoob991199.PlayerGui.ShopGUI.StrongSwordButton.Script:5: attempt to index nil with 'Backpack' 

Any ideas?
(I’m a beginner)

if this is a localscript you can’t access ServerStorage
and if this is a serverscript you should handle the MouseButton1Click Event on the client instead

How would I handle the event on the client?? Edit: Like what does that mean?

local Player = game.Players.LocalPlayer
local button = script.Parent
button.MouseButton1Click:Connect(function()
    local backpack = Player:WaitForChild("Backpack")
    if backpack then
      print("Found Player Backpack")
    end
end)
2 Likes

if you’re dealing with GUIs you should handle stuff locally using LocalScripts

so should this code be in a localscript? Cause when I try it with a server script it returns

Players.UrAnoob991199.PlayerGui.ShopGUI.StrongSwordButton.Script:4: attempt to index nil with 'WaitForChild' 

yes it should be a localscript

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