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

how to fix attempt to index nil with 'Value'?

Asked by 2 years ago
Edited 2 years ago
game.Players.PlayerAdded:Connect(function(Player)

    Player.Backpack:FindFirstChild("Oddity").Value = game.ServerStorage.Odditys.Oddity1

    if Player.Backpack:FindFirstChild("Oddity").Value == game.ServerStorage.Odditys.Oddity1 or game.ServerStorage.Odditys.Oddity2 then

        local Oddity = Player.Backpack.Oddity.Value
        Oddity:Clone().Parent = Player.Backpack

    end

end)

ERROR = ServerScriptService.DataService:3: attempt to index nil with 'Value' line = 3 the value is an object value

0
It means the object doesnt exist in serverstorage. Like the value you are trying to copy from voidofdeathfire 148 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

This is because you are using .Value but the :FindFirstChild("Oddity") may not find Oddity and therefore it returns nil. It's the same as saying nil.Value hence the error.

To fix this you should try :FindFirstChild() and on the next line check if it wasn't nil

game.Players.PlayerAdded:Connect(function(Player)
    local oddity = Player.Backpack:FindFirstChild("Oddity")
    if oddity then -- check if it's not nil then move on
        oddity.Value = game.ServerStorage.Odditys.Oddity1

        if oddity.Value == game.ServerStorage.Odditys.Oddity1 then
            -- whatever
        end
    else
        warn("Could not find Oddity in player backpack")
    end
end)
Ad

Answer this question