Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Attempt to index value nil with Backpack?

Asked by 3 years ago
Edited 3 years ago

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!

01local nameval = script.Parent.valn
02script.Parent.ClickDetector.MouseClick:Connect(function(player)
03    local val = nameval.Value
04    local plr = game.Players:FindFirstChild(val)
05    script.Parent.beep:Play()
06    for i,v in pairs(player.Backpack:GetChildren()) do
07        v:Clone().Parent = plr.Backpack
08    end
09        if game.ReplicatedStorage.orders.order.Value == 1 then
10        game.ReplicatedStorage.orders.order.Value = game.ReplicatedStorage.orders.order.Value -1
11    end
12end)

1 answer

Log in to vote
0
Answered by 3 years ago

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:

01local nameval = script.Parent.valn
02script.Parent.ClickDetector.MouseClick:Connect(function(player)
03    local val = nameval.Value
04    local plr = game.Players:FindFirstChild(val)
05    script.Parent.beep:Play()
06    for i,v in pairs(player:FindFirstChildOfClass("Backpack"):GetChildren()) do
07    if v:IsA("Tool") then
08            v:Clone().Parent = plr.Backpack
09    end
10    end
11        if game.ReplicatedStorage.orders.order.Value == 1 then
12        game.ReplicatedStorage.orders.order.Value = game.ReplicatedStorage.orders.order.Value -1
13    end
14end)

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

Ad

Answer this question