Attempt to index nil with "Backpack"

I would like to find out how to access the backpack in a script in ServerScriptService

This was weird. It works on my other games, but not this one.
So my script is just a function that gives the player a sword. The issue is that it can’t find “Backpack”

The script i use was almost identical to a script i used before, and the script I used before worked, but this one didn’t. (it’s not exactly the same, i had to change it up for my game.)

Script:

function CloneTool(player, object)
	local Backpack = player:FindFirstChild("Backpack")
	if game.ReplicatedStorage.Tools:FindFirstChild(object) then
		local c = game.ReplicatedStorage.Tools:FindFirstChild(object):Clone()
		c.Parent = Backpack
	end
end


wait(2)
CloneTool("Sword")

No matter how I format it, I tried doing

game.Players.LocalPlayer.Backpack

but that still didn’t work. There always seems to be a problem with finding the backpack and nothing else. I looked on the devhub, and I still couldn’t find anything. I just need another way to access the backpack, because the usual way I do it doesn’t work.

image
this is the error, It just can’t find the backpack for some reason. Thanks in advance.

3 Likes

You can’t use LocalPlayer in a ServerScript, to get the player in a server script use a playerAdded Event with the parameter ‘player’ and clone to tool like this:

game.Players.PlayerAdded:Connect(function(player)
--do the rest of the code here
end)

3 Likes

Thank you! This worked

(30 chars)

1 Like

You can’t use Client-Sided methods in Server-Sided, your problem is, you just scripted this as Client-Sided in a normal Script. Try using PlayerAdded function.