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

attempt to index nil with 'Backpack' any idea why this happens?

Asked by 2 years ago
script.Parent.Triggered:Connect(function(players)
    players.PlayerGui.ItemGui.Enabled = true
    local gui = players.PlayerGui.ItemGui
    gui.Frame.Username.Text = "None"
    script.Parent.Parent.Parent.TouchPart.Touched:Connect(function(hit)
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if player then
            gui.Frame.Username.Text = player.Name
        end
        print(player)
        local backpack = player.Backpack --the problem is this line
        if backpack:GetChildren() then
            for i,v in ipairs(backpack:GetChildren()) do
                local guil = Instance.new("TextLabel")
                guil.AnchorPoint = Vector2.new(0.5, 0.5)
                local size = UDim2.new(0.5, 0, 0.05, 0)
                guil.Size = size
                guil.Name = v.Name
                guil.Text = v.Name
                guil.BackgroundTransparency = 1
                guil.Font = Enum.Font.Gotham
                guil.TextSize = 24
                guil.TextWrapped = true
                guil.Parent = gui.ScrollingFrame.List

            end
        end
    end)
    script.Parent.Parent.Parent.TouchPart.TouchEnded:Connect(function(hit)
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if player then
            gui.Frame.Username.Text = "None"
        end
    end)
end)

im trying to make a item scanner but it doesnt work and gave me this error any idea why and how can i fix it?

2 answers

Log in to vote
1
Answered by 2 years ago
if player then
    gui.Frame.Username.Text = player.Name 
end

change that to:

if player then
    gui.Frame.Username.Text = player.Name
else
    return
end
Ad
Log in to vote
-1
Answered by 2 years ago

Try changing

local backpack = player.Backpack

to

local backpack = player:WaitForChild("Backpack")
0
What if the player is nil? DevNetx 250 — 2y

Answer this question