Backpack is not valid member of Player

I have an issue. I was trying to make a script where a random tool is selected from ServerStorage and placed in the Player’s backpack, but I got this error:

“Backpack is not a valid member of Player “Players.ParkCityUSA” - Client - LocalScript:2”
Here’s the script, which is in StarterPlayerScripts.

local Players = game:GetService("Players") 
local backpack = Players.LocalPlayer.Backpack
local ServerStor = game:GetService("ServerStorage")

local servstorTools = {ServerStor.ClassicSword, ServerStor.MP5K, ServerStor.AdventurerSword}
local randIndex = math.random(1, #servstorTools)
local backpack = Players.LocalPlayer:FindFirstChildOfClass("Backpack")

if backpack then
	for i = 1,3 do
		servstorTools[randIndex]:Clone().Parent = backpack
		wait(2)
	end

end

Thanks :smiley:

2 Likes

Why did you define backpack twice?

Line 2

local backpack = Players.LocalPlayer.Backpack

Line 7 (delete this)

local backpack = Players.LocalPlayer:FindFirstChildOfClass("Backpack")
2 Likes

You cannot access ServerStorage using a LocalScript.

2 Likes

@Auxicon I needed to delete line 2 in order to solve the bug. Must have slipped my mind.
@BirdieGoodMan What could be a possible solution to this? Should I put it somewhere else?

2 Likes

It seems that on line 2, the backpack is not found. Since this is a LocalScript, it is likely that the backpack has not loaded in yet.

local Players = game:GetService("Players").LocalPlayer
repeat task.wait() until Players.Character -- Not recommended, but it will still yield for the player character
local backpack = Players.Backpack

As for your ServerStorage problem, the client has access to the ReplicatedStorage. Put all your models there if you want to reference them from the client

4 Likes

You can put the tools in ReplicatedStorage as @Auxicon said.

3 Likes

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