Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index value nil with Backpack?

Hi! i have a part that contains a string value with the name of a player. When the button is pressed, i want it to clone the tools in the player that pressed its backpack and then put them into the player that has their name in the strvalue. However, im getting: "Attempt to index value nil with "Backpack".". I'm really confused! Please Help!

local nameval = script.Parent.valn
script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local val = nameval.Value
	local plr = game.Players:FindFirstChild(val)
	script.Parent.beep:Play()
	for i,v in pairs(player.Backpack:GetChildren()) do
		v:Clone().Parent = plr.Backpack
	end
		if game.ReplicatedStorage.orders.order.Value == 1 then
		game.ReplicatedStorage.orders.order.Value = game.ReplicatedStorage.orders.order.Value -1
	end
end)

1 answer

✓ Accepted answer

Your code seems fine. I'm assuming the player on line 6 is defined earlier in the script, so whatever.

It could be possible that your Backpack was renamed somehow? Or maybe it's just lua or Roblox being silly, so we'll try finding it based on the Backpack class, instead of the name as the Backpack object in the Player is classed also as 'Backpack'.

This should work:

local nameval = script.Parent.valn
script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local val = nameval.Value
    local plr = game.Players:FindFirstChild(val)
    script.Parent.beep:Play()
    for i,v in pairs(player:FindFirstChildOfClass("Backpack"):GetChildren()) do
	if v:IsA("Tool") then
        	v:Clone().Parent = plr.Backpack
	end
    end
        if game.ReplicatedStorage.orders.order.Value == 1 then
        game.ReplicatedStorage.orders.order.Value = game.ReplicatedStorage.orders.order.Value -1
    end
end)

I also added a check to make sure we're copying TOOLS only, not any scripts for any reason. Have a good day/night!

If it still doesn't work, hit me up on Discord: Undaub#4107

This discussion is preserved as it was. New answers, comments, and votes are closed.