Unable to access player's backpack

I’m am currently trying to make a kind of loadout system for a game Im working on.
Anyway I keep getting an error when trying to access the player’s backpack, " attempt to index nil with ‘FindFirstChild’ ", " attempt to index nil with ‘WaitForChild’ " etc…

Hopefully some people who are smarter than me can help out.

local player = game:GetService("Players").LocalPlayer

local primary = game.ReplicatedStorage.Weapons:WaitForChild("AK74N") 
local secondary = game.ReplicatedStorage.Weapons:WaitForChild("X16") 

local function loadoutGive(player)
	primary:Clone()
	primary.Parent = player:WaitForChild("Backpack") --error
	secondary:Clone()
	secondary.Parent = player:WaitForChild("Backpack") --error
end

game.ReplicatedStorage.RemoteEvents.RemoteEventPlay.OnServerEvent:Connect(function(player) 
	loadoutGive()
end)

Any and all help is appreciated!

1 Like

You’re not actually passing the player as an argument when calling the loadoutGive() function.

1 Like

I fixed this in my script and its still coming up with the same error

What about on the local script then. How are you firing the remote events?

maybe try removing the player variable at the start!

this is on a server script so yea

removed it, same error popping up…

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Parent.MainFrame.Visible = false
	game.ReplicatedStorage.RemoteEvents.RemoteEventPlay:FireServer()
end)

What’s your script type? You can’t use LocalPlayer if it isn’t a LocalScript.

EDIT: Another thing I notice is that you aren’t passing a player argument to loadoutGive but have support for one in the function.

1 Like

Im currently using a server script

try this, uses all suggestions above

local primary = game.ReplicatedStorage.Weapons:WaitForChild("AK74N") 
local secondary = game.ReplicatedStorage.Weapons:WaitForChild("X16") 

local function loadoutGive(player)
	primary:Clone()
    print(player.Name) --checking to see it works
	primary.Parent = player:WaitForChild("Backpack") --error
	secondary:Clone()
	secondary.Parent = player:WaitForChild("Backpack") --error
end

game.ReplicatedStorage.RemoteEvents.RemoteEventPlay.OnServerEvent:Connect(function(player) 
	loadoutGive(player)
end)

also couldnt you just say

game.ReplicatedStorage.RemoteEvents.RemoteEventPlay.OnServerEvent:Connect(loadoutGive) 
1 Like

This works, and never thought of the second part. Thanks!

1 Like