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!

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

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:

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

Ad

Answer this question